/*
*********************************************************************************************************
*                                     MICIRUM BOARD SUPPORT PACKAGE
*
*                             (c) Copyright 2007; Micrium, Inc.; Weston, FL
*
*               All rights reserved.  Protected by international copyright laws.
*               Knowledge of the source code may NOT be used to develop a similar product.
*               Please help us continue to provide the Embedded community with the finest
*               software available.  Your honesty is greatly appreciated.
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*
*                                        BOARD SUPPORT PACKAGE
*
*                                     ST Microelectronics STM32
*                                              with the
*                                   STM3210B-EVAL Evaluation Board
*
* Filename      : bsp.c
* Version       : V1.00
* Programmer(s) : Brian Nagel
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/

#define  BSP_GLOBALS
#include "includes.h"

/*本地全局变量定义*/
REVDEVCTRL  revDevCtrl[2];
REVDEVCTRL* 	pReadDevCtrl[1];
REVDEVCTRL*	pWriteDevCtrl[1];
UCHAR		receiveBuff[2][MAXRECEBUFF];

unsigned char  SFFContdataGetFlag;
unsigned char  SFFRealdataGet_Count;
unsigned char  SFFConstdataGet_Count;
unsigned char  ucSFFPresentAlarmCount;
unsigned char  ucLOSAlarmCount;

unsigned char  ucInputLowPowerAlarmCount;
unsigned char  ucOutputPowerAlarmCount;
unsigned char  ucPumpCurrentAlarmCount;
unsigned char  ucPumpTmpAlarmCount;

unsigned char  ucInputPowerAlarmCount[16];
unsigned char  ucPOWERAlarmCount[3];

u16 TIM2LSTXDISABLE_Count = 0;
u16 TIM2SYNCSW_Count = 0;
u16 TIM2MFSWDATASave_Count = 0;

ALARMTHRESHOLDS	  alarmThresholds;
ALARMHYSTERESIS   alarmHysteresis;
ALARMSTATUSINFO	  CardStatusInfo;
ALARMSTATUSINFO		alarmStatusInfo;
CANBUS   					CANBus;
ONS3000ECARD 			ONS3ECard;
ADCSAMPLE ADCSample;
__IO uint16_t ADC1ConvVal_CD4052[ADCHANNELNUM][ADC_Sample_Num];
ADCONVERT	AdcChannelData;
ADCONVERT	AdcChannelDataMax;
PG_TYPE 	PG;

ALARMSTATUSINFO	SFFStatusInfo;
ALARMINFO	alarmInfo;

u16 TIM2LEDAF_IND_Count = 0;

//unsigned short CardcmdObject[4] = {0x0100,0x0200,0x0300,0x0400};
unsigned short CardcmdObject[3] = {0x0100,0x0200,0x0300};

static const char *FirmwareRevision ="1.03";
static const char *HardwareRevision ="1.00";

unsigned short	PumpPowerMaxValue;


/*外部全局变量定义*/
extern char cStartFlag[2];
extern char downloadFirmareEnable;
extern char downloadParaEnable;
extern unsigned char	baudRateValue;
extern unsigned char	CurUseUSARTID;
extern unsigned char	BootVer;
extern unsigned char UpgradeInterface;
extern unsigned char UpgradeType;
extern unsigned char 	RamanBallenceFlag;
extern unsigned char 	RamanPump1BallenceFlag;
extern unsigned char 	switchSGM3157;
extern unsigned short 	modulePowerTrace;
extern unsigned short 	pump1PowerTrace;
extern unsigned short 	Pump1CurrentTrace;

extern unsigned char	 ECHOEnableFlag;

extern u32 TIM2BACK_SW_Count[2];
extern u32 TIM2SWMODEReover_Count[2];
extern u16 EEPROMBackup_Count;
extern u16 TIM2MFSWHOLDOFF_Count[2];

/*外部函数声明*/
extern void EEPRomInit( void );
extern UCHAR ReadEEPRom( USHORT );
extern STATUS WriteEEPRom( USHORT , UCHAR );
extern void USART1Print( char * );
extern void USART3Print( char * );
extern void USARTPrint( char *pOutput , char USARTID);
extern unsigned int IntegerToLogarithm  (unsigned int uiIntergerData );
extern void EEPROMdataCheck( void );

extern void UDPProcess( void );
extern void PrintChar( char );

extern unsigned int IntegerToLogarithm  (unsigned int uiIntergerData );

/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                         Global Functions
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                         BSP INITIALIZATION
*
* Description : This function should be called by your application code before you make use of any of the
*               functions found in this module.
*
* Arguments   : none
*********************************************************************************************************
*/

void BitSet( u16 *ptr, char index )
{
	*ptr |= ( 1 << index );
}

void BitClear( u16 *ptr, char index )
{
	*ptr &= ~( 1 << index );
}

void BigtoLittleS16(short *data)
{
	u8 TempByteHig,TempByteLow;
	SHORTVAR	saveData;
	
	saveData.sWord = *data;
	
	TempByteHig = saveData.ucByte[1];
	TempByteLow = saveData.ucByte[0];
	
	saveData.ucByte[1] = TempByteLow;
	saveData.ucByte[0] = TempByteHig;
	
	*data = saveData.sWord;
}

void BigtoLittleU16(u16 *data)
{
	u8 TempByteHig,TempByteLow;
	USHORTVAR	saveData;
	
	saveData.usWord = *data;
	
	TempByteHig = saveData.ucByte[1];
	TempByteLow = saveData.ucByte[0];
	
	saveData.ucByte[1] = TempByteLow;
	saveData.ucByte[0] = TempByteHig;
	
	*data = saveData.usWord;
}

/*CRC16校验*/
u16 crc16_CCITT(u8 *ptr1,u8 len)
{
  u8 i;
  u16 crc=0;
  while(len--)
        {
		  for(i=0x80;i!=0;i>>=1)
		      {
			    if((crc&0x8000)!=0)
				   {
				     crc<<=1;
					 crc^=0x1021;
				   }
				 else crc<<=1;
				 if((*ptr1&i)!=0)
				    crc^=0x1021;
			  }
			  ptr1++;
		}
	 return(crc); 
}

void Delay(unsigned int nCount)
{
  for(; nCount != 0; nCount--);
}

void Delay_100us(unsigned int nCount)
{
	u32 TimeDelay;
	
	for(; nCount != 0; nCount--)
	{
		//TimeDelay = 1650;
		TimeDelay = 3800;
		for(; TimeDelay != 0; TimeDelay--);
	}
}

void BoardReset(void)
{
	//Delay(4000);
	Delay_100us(10);
	
	TIM2_Disable;
	TIM3_Disable;
	TIM3_IT_Disable;
	usart_disable(USART0);
	usart_interrupt_disable(USART0, USART_INT_RBNE);
	adc_disable(ADC0);
	spi_disable(SPI2);
	SysTick->CTRL=0x00;     //关闭SysTick
	can_interrupt_disable(CAN0,CAN_INT_RFNE0);	  //关闭FIFO0接收中断
	can_interrupt_disable(CAN1,CAN_INT_RFNE1);	  //关闭FIFO1接收中断
	enet_interrupt_disable(ENET_DMA_INT_NIE);  //禁止以太网接收中断	
	enet_interrupt_disable(ENET_DMA_INT_RIE);

	NVIC_SystemReset();
}



void CommDevCtrlInit( void )
{
	/*USART1 Buffer初始化*/
	revDevCtrl[0].next = ( REVDEVCTRL * )&revDevCtrl[1];
	revDevCtrl[1].next = ( REVDEVCTRL * )&revDevCtrl[0];
	
	pReadDevCtrl[0] = revDevCtrl[1].next;
	pWriteDevCtrl[0] = pReadDevCtrl[0];
	
	pWriteDevCtrl[0]->pRevBuff = ( UCHAR *)receiveBuff[0];
	pWriteDevCtrl[0]->pWritePtr = pWriteDevCtrl[0]->pRevBuff;
	pWriteDevCtrl[0]->writeIndex = 0;
	pWriteDevCtrl[0]->packetLength = 0;
	pWriteDevCtrl[0]->maxLength = MAXRECEBUFF;
	pWriteDevCtrl[0]->receBuffStatus = BUFF_EMPTY;

	pWriteDevCtrl[0]->next->pRevBuff = ( UCHAR *)receiveBuff[1];
	pWriteDevCtrl[0]->next->pWritePtr = pWriteDevCtrl[0]->next->pRevBuff;
	pWriteDevCtrl[0]->next->writeIndex = 0;
	pWriteDevCtrl[0]->next->packetLength = 0;
	pWriteDevCtrl[0]->next->maxLength = MAXRECEBUFF;
	pWriteDevCtrl[0]->next->receBuffStatus = BUFF_EMPTY;		
}

void RCC_Configuration(void)
{         
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_GPIOB);
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_GPIOE);
	
	rcu_periph_clock_enable(RCU_SPI2);
	rcu_periph_clock_enable(RCU_ADC0);

  rcu_periph_clock_enable(RCU_USART0);
	
	rcu_periph_clock_enable(RCU_TIMER1);
	rcu_periph_clock_enable(RCU_TIMER2);
	//rcu_periph_clock_enable(RCU_TIMER11);
	rcu_periph_clock_enable(RCU_SYSCFG);  //使能SYSCFG时钟
}



/*
*********************************************************************************************************
*                                     DISABLE ALL INTERRUPTS
*
* Description : This function disables all interrupts from the interrupt controller.
*
* Arguments   : None.
*
* Returns     : None.
*********************************************************************************************************
*/
void  BSP_IntDisAll (void)
{
  // CPU_IntDis();
}

/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                         uC/OS-II Timer Functions
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                       TICKER INITIALIZATION
*
* Description : This function is called to initialize uC/OS-II's tick source (typically a timer generating
*               interrupts every 1 to 100 mS).
*
* Arguments   : none
*
* Note(s)     : 1) The timer is setup for output compare mode BUT 'MUST' also 'freerun' so that the timer
*                  count goes from 0x00000000 to 0xFFFFFFFF to ALSO be able to read the free running count.
*                  The reason this is needed is because we use the free-running count in uC/OS-View.
*********************************************************************************************************
*/




void InitADCGPIO(void)
{
	uint32_t ConfigPins;
	
	//PA0,PA2,PA3,PA5,PA6配置成模拟输入方式用于ADC采样
	ConfigPins = GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_5 | GPIO_PIN_6;
  gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,ConfigPins);
	
	//PC0,PC2,PC3配置成模拟输入方式用于ADC采样
	ConfigPins = GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_3;
  gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,ConfigPins);
}



void adc_config(void)
{
	RCU_APB2RST|=1<<8;   	//ADC复位
	RCU_APB2RST&=~(1<<8);	//ADC复位结束
	
	ADC_SYNCCTL &= ~(0X1F<<0);  //ADC同步模式失能，所有的ADC都独立工作
	ADC_SYNCCTL &= ~(7<<16);
  ADC_SYNCCTL |= 1<<16;	    //ADC转换时钟 Pacdclk=PCLK2/4=75MHZ/4=18.75MHZ

	ADC_CTL0(ADC0)&=~(1<<8);    //非扫描模式
	ADC_CTL1(ADC0)&=~(1<<1);    //单次转换模式
	ADC_CTL1(ADC0)&=~(3<<28);	  //ETMRC 禁止规则通道外部触发 软件触发ADC转换
	ADC_CTL1(ADC0)&=~(0xF<<24); //ETSRC 外部触发选择定时器0
	ADC_CTL1(ADC0)&=~(1<<11);   //右对齐
	ADC_RSQ0(ADC0)&=~(0XF<<20); //1个转换在规则序列中，也就是规则通道数只有1个

	//设置通道的采样时间
	ADC_SAMPT1(ADC0)&=0x00000000;  //通道采样时间清空
	ADC_SAMPT0(ADC0)&=0x00000000;  //通道采样时间清空
	//通道0,2,3,5,6,10,12,13 采样周期配置为15个周期
	ADC_SAMPT0(ADC0)|=(ADCSampleCycle<<0)|(ADCSampleCycle<<6)|(ADCSampleCycle<<9);	
	ADC_SAMPT1(ADC0)|=(ADCSampleCycle<<0)|(ADCSampleCycle<<6)|(ADCSampleCycle<<9)|(ADCSampleCycle<<15)|(ADCSampleCycle<<18);

	ADC_CTL1(ADC0)|=1<<0;	      //开启ADC转换器
	ADC_CTL1(ADC0)|=1<<3;        //校准初始化开始
	while(ADC_CTL1(ADC0)&1<<3);  //等待校准初始化结束
	//该位由软件设置并由硬件清除。在校准寄存器被初始化后该位被清除
	ADC_CTL1(ADC0)|=1<<2;        //开启ADC校准
	while(ADC_CTL1(ADC0)&1<<2);  //等待校准结束
	//该位由软件设置开始校准，并在校准结束时由硬件清除
}


void InitInOutGPIO(void)
{
	uint32_t ConfigPins;
  //gpio_pin_remap_config(GPIO_SWJ_DISABLE_REMAP,ENABLE);   //SWD与JTAG功能都禁止
  //gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP,ENABLE); //使能SWD功能,禁止JTAG功能
	
	//设置GPIOA端口的引脚输出模式 PA11->LS_LED_IND,PA12->TX_LED_IND,PA15->AT25080_CS
	ConfigPins = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_15;
	gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, ConfigPins);
  gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, ConfigPins);
	
	//设置GPIOB端口的引脚输出模式 PB3->GAIN_SEL_CH20,PB4->GAIN_SEL_CH12,PB7->GAIN_SEL_CH10,PB14->ModeSel,PB15->PUMP_OFF
	ConfigPins = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_7 | GPIO_PIN_14 | GPIO_PIN_15;
	gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, ConfigPins);
  gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, ConfigPins);

	//设置GPIOC端口的引脚输入模式 PC1->CUR_CH_TX,PC9->CUR_CH_RX,PC13->SC_SLOTID2,PC14->SC_CARD_INSERT
	ConfigPins = GPIO_PIN_1 | GPIO_PIN_9 | GPIO_PIN_13 | GPIO_PIN_14; 
  gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, ConfigPins);
  gpio_output_options_set(GPIOC, GPIO_PUPD_PULLUP, GPIO_OSPEED_50MHZ, ConfigPins);	
	
	//设置GPIOC端口的引脚输出模式 PC7->FSW_CTRL1,PC8->FSW_CTRL2,PC15->FeedDog
  ConfigPins = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_15;
	gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, ConfigPins);
  gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, ConfigPins);
	
	//设置GPIOD端口的引脚输入模式 PD14->KEY2,PD15->KEY1
	ConfigPins = GPIO_PIN_14 | GPIO_PIN_15; 
  gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, ConfigPins);
  gpio_output_options_set(GPIOD, GPIO_PUPD_PULLUP, GPIO_OSPEED_50MHZ, ConfigPins);	
	
	//设置GPIOD端口的引脚输出模式 PD0->R2_LED_IND,PD1->R1_LED_IND,PD2->FAULT_LED,PD3->RUN_LED,PD4->PS_LED_IND,PD5->SWMODE_LED_IND,PD6->GAIN_SEL_CH22,PD7->GAIN_SEL_CH21,PD10->SF2507_RESET,PD12->TPC116S8_CS
  ConfigPins = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10 | GPIO_PIN_12;
	gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, ConfigPins);
  gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, ConfigPins);
	
	//设置GPIOE端口的引脚输入模式 PE3->SC_SLITID1,PE4->SC_SLITID3,PE5->SC_SLITID4,PE6->SC_SLITID0,PE9->SFP2_PRE,PE10->SFP2_LOS
	ConfigPins = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_9 | GPIO_PIN_10; 
  gpio_mode_set(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, ConfigPins);
  gpio_output_options_set(GPIOE, GPIO_PUPD_PULLUP, GPIO_OSPEED_50MHZ, ConfigPins);
	
	//设置GPIOE端口的引脚输出模式 PE0->GAIN_SEL_CH11,PE1->FSW_CTRL3,PE2->FSW_CTRL4,PE11->SFP2_TXDISABLE,PE12->GAIN_SEL_CH32,PE13->GAIN_SEL_CH30,PE14->GAIN_SEL_CH31
  ConfigPins = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14;
	gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, ConfigPins);
  gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, ConfigPins);
}

void InitSPIGPIO(void)
{
	uint32_t ConfigPins;
	spi_parameter_struct spi_init_struct;
	
  //SPI器件片选信号配置
  GT25C08CS_Set_Hig;    //PA15-->GT25C08_CS
 
	//GPIOC10,11,12 初始化为SPI3接口
	ConfigPins = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;//PC10~12复用功能输出
	gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, ConfigPins);
  gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, ConfigPins);
	
	gpio_af_set(GPIOC, GPIO_AF_6, GPIO_PIN_10);  //PC10复用为SPI3
	gpio_af_set(GPIOC, GPIO_AF_6, GPIO_PIN_11);  //PC11复用为SPI3
	gpio_af_set(GPIOC, GPIO_AF_6, GPIO_PIN_12);  //PC12复用为SPI3
  
  /* deinitilize SPI and the parameters */
  spi_i2s_deinit(SPI2);
  spi_struct_para_init(&spi_init_struct);

  /* configure SPI2 parameter */
  spi_init_struct.trans_mode           = SPI_TRANSMODE_FULLDUPLEX;
  spi_init_struct.device_mode          = SPI_MASTER;
  spi_init_struct.frame_size           = SPI_FRAMESIZE_8BIT;
  spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  spi_init_struct.nss                  = SPI_NSS_SOFT;
  spi_init_struct.prescale             = SPI_PSC_16;
  spi_init_struct.endian               = SPI_ENDIAN_MSB;
  spi_init(SPI2, &spi_init_struct);
    
  spi_nss_internal_high(SPI2);
	
	spi_enable(SPI2);
}


void GPIO_Configuration(void)
{
  InitInOutGPIO();
	InitSPIGPIO();
	InitADCGPIO();
}


void FeedDogTps3823DBVR(void)
{       
	if ( FeedDog_STA == 1 )
	{
		FeedDog_Set_Low;
	}
	else
	{
		FeedDog_Set_Hig;
	}
}

void setBaud(UCHAR baud)
{
	ULONG	baudVal; 
	
	switch(baud)
	{
 		case 1:
			baudVal = 9600;
		break;
		case 2:
			baudVal = 19200;
		break;	
		case 3:
			baudVal = 38400;
		break;
		case 4:
			baudVal = 57600;
		break;	
		case 5:
			baudVal = 115200;
		break;
		default:
 			baudVal = 115200;
 			baudRateValue= 5;
 		break;
	}
	
	/* enable GPIO clock */
  rcu_periph_clock_enable(RCU_GPIOA);

  /* enable USART clock */
  rcu_periph_clock_enable(RCU_USART0);

  /* configure the USART0 Tx pin and USART0 Rx pin */
  gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);
  gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);
    
  /* configure USART0 Tx as alternate function push-pull */
  gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
  gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);

  /* configure USART0 Rx as alternate function push-pull */
  gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
  gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
	
	//baudVal = 57600;
  /* USART configure */
  usart_deinit(USART0);
  usart_baudrate_set(USART0, baudVal);
  usart_word_length_set(USART0, USART_WL_8BIT);
  usart_stop_bit_set(USART0, USART_STB_1BIT);
  usart_parity_config(USART0, USART_PM_NONE);
  usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
  usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
  usart_receive_config(USART0, USART_RECEIVE_ENABLE);
  usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
  usart_enable(USART0);
	
	/* USART interrupt configuration */
  //nvic_irq_enable(USART0_IRQn, 3, 2);
	nvic_irq_enable(USART0_IRQn, 0, 2);
	
  /* disable USART TBE interrupt */  
	/* enable USART RBNE interrupt */ 
  usart_interrupt_disable(USART0, USART_INT_TBE);
  usart_interrupt_enable(USART0, USART_INT_RBNE);
}

void MisDeviceInit(void)
{
	EEPRomInit();	
}

void EEPROMDATABackup( void )
{
	u16 ucI;
	
	for(ucI=0; ucI<EEPROMDATALen; ucI++)
	{
		WriteEEPRom( EEPROM_Backup_ADDR + ucI , ONS3ECard.EEPROMBuff[ucI] );
		
		//if(ONS3ECard.HOLDOFSWFlag == TRUE || ONS3ECard.BACKSWStartFlag == 0x01 || ONS3ECard.SWMODERecoverStartFlag == TRUE)
		if(ONS3ECard.MFSWUpdateFlag == 1 || ONS3ECard.HOLDOFSWFlag == TRUE || ONS3ECard.BACKSWStartFlag == 0x01 || ONS3ECard.SWMODERecoverStartFlag == TRUE)
		break;
	}
}

void GetParaFromCode(void)
{
	uchar	*getValPtr;

	getValPtr = ( uchar * )FLASHPARA_ADDR;
	ONS3ECard.waveNum = *getValPtr++;
	ONS3ECard.pumpNum = *getValPtr++;
	
	memcpy( ( char *)&ONS3ECard.K1, ( char * )SYSTEM_PARA_ADDR, 82 );
	
	PumpPowerMaxValue = ONS3ECard.PP1;
}

void UpdateONS3ECardPara( void )
{
	SHORTVAR	getData;
	USHORTVAR	getData_u;
	u16 ucI,ucII,ucIII;
	
	//alarmthresholds	for PD
	for(ucI=0; ucI<7; ucI++)
	{
		getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOS + 2*ucI ];	
		getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOS + 2*ucI + 1];	
		alarmThresholds.LOS[0][ucI] = getData.sWord;
		
		getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOSVAL + 2*ucI ];	
		getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOSVAL + 2*ucI + 1];	
		alarmThresholds.LOSVAL[ucI] = getData.sWord;

	}

	
	//PDPARAOFFSET	
	for(ucI=0; ucI<6; ucI++)
	{
		for(ucII=0; ucII<3; ucII++)
		{
			getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PDCALPARA_OFFSET + 6*ucI + 2*ucII];	
			getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PDCALPARA_OFFSET + 6*ucI + 2*ucII + 1];	
			ONS3ECard.PD_calOffset[ucI][ucII] = getData.sWord;
		}
	}
	
	//alarmhysteresis for PD
	for(ucI=0; ucI<7; ucI++)
	{
		getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS + 2*ucI ];	
		getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS + 2*ucI + 1];	
		alarmHysteresis.LOS[ucI] = getData.sWord;
	}
	
	
	//PD_CAL para
	for(ucI=0; ucI<7; ucI++)
	{
		if(ucI == 6)
		{
			getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PDCAL_PARA + 8*ucI];	
			getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PDCAL_PARA + 8*ucI + 1];	
			ONS3ECard.PD_cal[ucI][0] = getData.sWord;			
		}
		else
		{
			for(ucII=0; ucII<4; ucII++)
			{
				getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PDCAL_PARA + 8*ucI + 2*ucII];	
				getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PDCAL_PARA + 8*ucI + 2*ucII + 1];	
				ONS3ECard.PD_cal[ucI][ucII] = getData.sWord;
			}
		}
	}
	
	//PG para	
	for(ucI=0; ucI<1; ucI++)
	{
		PG.SwitchCMDType[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_SwitchCMDType + ucI ];
		PG.SWMODERecover[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_SWMODERecover + ucI ];
		PG.BackSWMode[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_BACKSWMode + ucI ];
		PG.KEYEnable[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_KEYENABLE + ucI ];
		PG.TXCycle[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_TXCycle + ucI ];
		PG.ProtectionDirection[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_ProtectionDir + ucI ];
		PG.AUTOSW_TYPE[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_AUTOSWTYPE + ucI ];
		
		getData_u.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PG_HoldOFF + 2*ucI ];	
		getData_u.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PG_HoldOFF + 2*ucI + 1];	
		PG.HoldOFF[ucI] = getData_u.usWord;
		
		getData_u.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PG_RecoverTime + 2*ucI ];	
		getData_u.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PG_RecoverTime + 2*ucI + 1];	
		PG.SWMODERecoverTime[ucI] = getData_u.usWord;
		
		getData_u.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PG_RestoreTime + 2*ucI ];	
		getData_u.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PG_RestoreTime + 2*ucI + 1];	
		PG.WaitToRestoreTime[ucI] = getData_u.usWord;
		
		PG.PrimaryPort[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_PrimaryPort + ucI ];
		PG.SecondaryPort[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_SecondPort + ucI ];
		PG.SelectedPort[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PG_SelectPort + ucI ];
		
		PG.SwitchCMDTypePre[ucI] = PG.SwitchCMDType[ucI];
		
		for(ucII=0; ucII<2; ucII++)
		{

			for(ucIII=0; ucIII<2; ucIII++)
			{			
				getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PG_TXIL + 8*ucI + 4*ucII + 2*ucIII];	
				getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PG_TXIL + 8*ucI + 4*ucII + 2*ucIII + 1];	
				PG.OptSwitchTXIL[ucI][ucII][ucIII] = getData.sWord;
			}
			
			getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PG_RXIL + 4*ucI + 2*ucII];	
			getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PG_RXIL + 4*ucI + 2*ucII + 1];	
			PG.OptSwitchRXIL[ucI][ucII] = getData.sWord;
			
			getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PG_ABSTHR + 4*ucI + 2*ucII];	
			getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PG_ABSTHR + 4*ucI + 2*ucII + 1];	
			PG.ABSTHR[ucI][ucII] = getData.sWord;
			
			getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PG_OFFSETTHR + 4*ucI + 2*ucII];	
			getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PG_OFFSETTHR + 4*ucI + 2*ucII + 1];	
			PG.OFFSETTHR[ucI][ucII] = getData.sWord;
		}
	}
	
		
	for(ucI=0; ucI<16; ucI++)
	{
		ONS3ECard.PN[0][ucI] = ONS3ECard.EEPROMBuff[ ADDR_CARD_PN + ucI ];	
		ONS3ECard.SN[0][ucI] = ONS3ECard.EEPROMBuff[ ADDR_CARD_SN + ucI ];
	}
	
	for(ucI=0; ucI<CardDESCRIPLen; ucI++)
	{
		ONS3ECard.DESCRIP[0][ucI] = ONS3ECard.EEPROMBuff[ ADDR_CARD_DESCRIP + ucI ];	
	}
	
	FeedDogTps3823DBVR();	 

	for(ucI=0; ucI<CardDATELen; ucI++)
	ONS3ECard.DATE[0][ucI] = ONS3ECard.EEPROMBuff[ ADDR_CARD_DATE + ucI ];
	
	for(ucI=0; ucI<SFPPORTS; ucI++)
	{

		ONS3ECard.SFFModule.PortENABLE[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PORT_ENABLE + ucI ];
		ONS3ECard.SFFModule.PortLOSOFF[ucI] = ONS3ECard.EEPROMBuff[ ADDR_PORT_LOSOFF + ucI ];
		
		getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOS + 2*ucI];	
		getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOS + 2*ucI + 1 ];	
		ONS3ECard.SFFModule.LOSTHR[ucI] = getData.sWord;
	}
	
	for(ucI=0; ucI<4; ucI++)
	{
		lwipdev.ip[ucI] = ONS3ECard.EEPROMBuff[ ADDR_NET_IP + ucI ];
		lwipdev.remoteip[ucI] = ONS3ECard.EEPROMBuff[ ADDR_REMOTENET_IP + ucI ];
		lwipdev.netmask[ucI] = ONS3ECard.EEPROMBuff[ ADDR_NET_MASK + ucI ];
		lwipdev.gateway[ucI] = ONS3ECard.EEPROMBuff[ ADDR_NET_GATEWAY + ucI ];
	}
	
	for(ucI=0; ucI<6; ucI++)
	{
		lwipdev.mac[ucI] = ONS3ECard.EEPROMBuff[ ADDR_NET_MAC + ucI ];
	}
	
	ECHOEnableFlag = ONS3ECard.EEPROMBuff[ ADDR_ECHO ];
	ONS3ECard.Type[0] = ONS3ECard.EEPROMBuff[ ADDR_CARD_TYPE ];
	ONS3ECard.FUN[0] = ONS3ECard.EEPROMBuff[ ADDR_CARD_FUNCTION ];

	ONS3ECard.PORTNUMS = 0x07;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_Signal ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_Signal + 1 ];	
	alarmThresholds.Signal[0] = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOW ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_LOW + 1 ];	
	alarmThresholds.Low[0] = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_ILD ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_ILD + 1 ];	
	alarmThresholds.ILD = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_TMP ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_TMP + 1 ];	
	alarmThresholds.TMP = getData.sWord;;
	
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_CARDMTL ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_CARDMTL + 1];	
	alarmThresholds.CardMTL[0] = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_CARDMTH ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_THRESHOLD_CARDMTH + 1];	
	alarmThresholds.CardMTH[0]= getData.sWord;
	
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_TOTAL_GAINSET ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_TOTAL_GAINSET + 1 ];	
	ONS3ECard.ramanGainSet[0] = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_WAVE1_PUMPPOWERSET ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_WAVE1_PUMPPOWERSET + 1 ];	
	ONS3ECard.wave1Powerset[0] = getData.sWord;
	
	ONS3ECard.pumpPowerSet = ONS3ECard.wave1Powerset[0];
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_PUMP1_CURRENT ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_PUMP1_CURRENT + 1 ];	
	ONS3ECard.pump1ISPSet[0] = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_Signal ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_Signal + 1 ];	
	alarmHysteresis.Signal = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_LOW ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_LOW + 1 ];	
	alarmHysteresis.Low = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_ILD ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_ILD + 1 ];	
	alarmHysteresis.ILD = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_CARDMTH ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_CARDMTH + 1];	
	alarmHysteresis.CardMTH = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_CARDMTL ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_CARDMTL + 1];	
	alarmHysteresis.CardMTL = getData.sWord;
	
	getData.ucByte[0] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_TMP ];	
	getData.ucByte[1] = ONS3ECard.EEPROMBuff[ ADDR_HYSTERESIS_TMP + 1 ];	
	alarmHysteresis.TMP = getData.sWord;
	
	ONS3ECard.OpMode[0] = ONS3ECard.EEPROMBuff[ADDR_OPMODE];
	
	baudRateValue = ONS3ECard.EEPROMBuff[ ADDR_BAUD ];
	
	ONS3ECard.EEPROMDataGetOKFlag = TRUE;
	
	FeedDogTps3823DBVR();		
}

void GetDataFromEEPROM( void )
{
	u16 ucI;
	
	ONS3ECard.EEPROMCalcuCheckSum = 0;
	ONS3ECard.EEPROMDATALEN = ADDR_PG_TXCycle + 1;
	
	for(ucI=0; ucI<EEPROMDATALen; ucI++)
	{
		ONS3ECard.EEPROMBuff[ucI] = ReadEEPRom( EEPROM_START_ADDR + ucI );
		ONS3ECard.EEPROMCalcuCheckSum += ONS3ECard.EEPROMBuff[ucI];
		
		if(ucI%32 == 0)
		FeedDogTps3823DBVR();
	}
	
	ONS3ECard.EEPROMSaveCheckSum = ReadEEPRom( ADDR_CHECKSUM );
	
	if(ONS3ECard.EEPROMCalcuCheckSum == ONS3ECard.EEPROMSaveCheckSum)
	{
		UpdateONS3ECardPara();  
	}
	else
	{
		ONS3ECard.EEPROMDataGetOKFlag = FALSE;
	}
 }

void GetEEPROMDataFromBackup( void )
{
	u16 ucI;
	
	for(ucI=0; ucI<EEPROMDATALen; ucI++)
	{
		ONS3ECard.EEPROMBuff[ucI] = ReadEEPRom( EEPROM_Backup_ADDR + ucI );
		
		if(ucI%32 == 0)
		FeedDogTps3823DBVR();
	}
	
	UpdateONS3ECardPara();
}

void ONS3ECardParaInit( void )
{
	u8 ucI;
	u8 SWDataLen,HWDataLen;
	
	SWDataLen = strlen(FirmwareRevision);
	HWDataLen = strlen(HardwareRevision);
	
	for(ucI=0; ucI<16; ucI++)
	{
		if(ucI<SWDataLen)
		ONS3ECard.SW[0][ucI] = FirmwareRevision[ucI];
		else
		ONS3ECard.SW[0][ucI] = 0x00;	
		
		if(ucI<HWDataLen)
		ONS3ECard.HW[0][ucI] = HardwareRevision[ucI];
		else
		ONS3ECard.HW[0][ucI] = 0x00;	
	}

	for(ucI=0; ucI<7; ucI++)
	{
		ONS3ECard.thresholdMin[ucI] = -7000;
		ONS3ECard.thresholdMax[ucI] = 3000;
		ONS3ECard.hystersisMin[ucI] = 0;
		ONS3ECard.hystersisMax[ucI] = 10000;
		
		ONS3ECard.PDStage1VolOffset[ucI] = 0;
	}
	
	ONS3ECard.thresholdCARDMTHMin = -500;
	ONS3ECard.thresholdCARDMTHMax = 1000;
	
	ONS3ECard.hystersisCARDMTHMin = 0;
	ONS3ECard.hystersisCARDMTHMax = 1000;
	
	ONS3ECard.thresholdCARDMTLMin = -500;
	ONS3ECard.thresholdCARDMTLMax = 1000;
	
	ONS3ECard.hystersisCARDMTLMin = 0;
	ONS3ECard.hystersisCARDMTLMax = 1000;
	
	SFFStatusInfo.alarmSTA.LOS_PD = 0x00;
	
	PG.KeySWFlag[0] = FALSE;
	PG.KeySWFlag[1] = FALSE;
	ONS3ECard.HOLDOFSWFlag = FALSE;
	ONS3ECard.BACKSWStartFlag = 0x00;
	ONS3ECard.SWMODERecoverStartFlag = FALSE;
	ONS3ECard.LSTXDISABLEFlag = FALSE;	
	ONS3ECard.LSOKFlag = FALSE;
	ONS3ECard.SYNCSWFlag = FALSE;
	PG.SwitchCMDTypeTemp = PG.SwitchCMDType[0];
	
	PG.SWCount[0] = 0x00;
	PG.SWCount[1] = 0x00;
	
	ONS3ECard.OutIndex = 0x00;
	
	ONS3ECard.UDPRecvDebug = 0;
	ONS3ECard.UDPSendDebug = 0;
	ONS3ECard.CANDebug = 0;
	
	ONS3ECard.Respond_sync_SW_Flag = FALSE;
	ONS3ECard.Request_remote_OptPwr_Flag = TRUE;
	ONS3ECard.Respond_remote_OptPwr_Flag = FALSE;
	ONS3ECard.R1_R2_OptPwr_LOS_count = 0x00;
	ONS3ECard.remote_OptPwr_Request_count = 0x00;
	ONS3ECard.remote_OptPwr_Respond_count = 0x00;
	ONS3ECard.UDP_Get_Remote_Info_Flag = TRUE;
	ONS3ECard.Request_remote_OptPwr_over_Flag = FALSE;
	ONS3ECard.Respond_remote_OptPwr_over_Flag = FALSE;
}

void sysRamClear( void )
{
	u8 ucI;
	
	downloadParaEnable = 0;
	downloadFirmareEnable = 0;
	
	ONS3ECard.loopNum = 0;
	
 	modulePowerTrace = 0;
 	pump1PowerTrace = 1000;  //防止上电时APC模式下停留时间过长
 	ONS3ECard.ramanGain = 0;
 	ONS3ECard.ramanASEPower = 0;
	
	Pump1CurrentTrace = 0;
 	
	for(ucI=0; ucI<6; ucI++)
	ucInputPowerAlarmCount[ucI] = 0;
	
 	ucInputLowPowerAlarmCount = 0;
 	ucOutputPowerAlarmCount = 0;
 	ucPumpCurrentAlarmCount = 0;
 	ONS3ECard.currentAlarm = 0;
 	
 	ONS3ECard.pumpStatus = PUMP_OFF;
 	ONS3ECard.SoftwareShutFlag = DISABLE;
 	RamanBallenceFlag = DISABLE;
	RamanPump1BallenceFlag = FALSE;

	ONS3ECard.PreOpMode = DISABLE_MODE;
	
	OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,0);
	PUMP1_OFF;
}

void sysRamInit( void )
{		
	CommDevCtrlInit();
	GetParaFromCode();
 	GetDataFromEEPROM();
	ONS3ECardParaInit();
}


void getADCConvData( void )
{
	uchar ucI,ucII;//ucIII;
	unsigned short adcMinValue,adcMaxValue;//adcValue;
	
	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道10
	ADC_RSQ2(ADC0)|=10;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[0][ucII] = ADC_RDATA(ADC0);		 	   
	}	

	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道3
	ADC_RSQ2(ADC0)|=3;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[1][ucII] = ADC_RDATA(ADC0);		 	   
	}	
	
	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道5
	ADC_RSQ2(ADC0)|=5;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[2][ucII] = ADC_RDATA(ADC0);		 	   
	}	
	
	for(ucI=0;ucI<3;ucI++)
	{
		adcMinValue = ADC1ConvVal_CD4052[ucI][0];
		adcMaxValue = ADC1ConvVal_CD4052[ucI][0];
		ADCSample.DATA[ucI] = 0;
		for (ucII=0;ucII<ADC_Sample_Num;ucII++)
		{						
			ADCSample.DATA[ucI] += ADC1ConvVal_CD4052[ucI][ucII];
			if( ADC1ConvVal_CD4052[ucI][ucII] < adcMinValue )
			adcMinValue = ADC1ConvVal_CD4052[ucI][ucII];		
			if( ADC1ConvVal_CD4052[ucI][ucII] > adcMaxValue )
			adcMaxValue = ADC1ConvVal_CD4052[ucI][ucII];		
		}
		ADCSample.DATA[ucI] -= adcMinValue;
		ADCSample.DATA[ucI] -= adcMaxValue;
		ADCSample.DATA[ucI] = ADCSample.DATA[ucI] / (ADC_Sample_Num - 2);
	}
}


void getADCConvData_Pump( void )
{
	uchar ucI,ucII;//ucIII;
	unsigned short adcMinValue,adcMaxValue;//adcValue;
	
	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道6
	ADC_RSQ2(ADC0)|=6;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[3][ucII] = ADC_RDATA(ADC0);		 	   
	}	

	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道0
	ADC_RSQ2(ADC0)|=0;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[4][ucII] = ADC_RDATA(ADC0);		 	   
	}	
	
	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道2
	ADC_RSQ2(ADC0)|=2;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[5][ucII] = ADC_RDATA(ADC0);		 	   
	}	
	
	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道12
	ADC_RSQ2(ADC0)|=12;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[6][ucII] = ADC_RDATA(ADC0);		 	   
	}	

	ADC_RSQ2(ADC0)&=0XFFFFFFE0;//规则序列1选择通道13
	ADC_RSQ2(ADC0)|=13;
	for(ucII=0;ucII<ADC_Sample_Num;ucII++)
	{		  			    
	 ADC_CTL1(ADC0)|=1<<30;       		//启动规则转换通道
	 while(!(ADC_STAT(ADC0)&1<<1));		//等待转换结束 
	 ADC1ConvVal_CD4052[7][ucII] = ADC_RDATA(ADC0);		 	   
	}		
	
	for(ucI=3;ucI<ADCHANNELNUM;ucI++)
	{
		adcMinValue = ADC1ConvVal_CD4052[ucI][0];
		adcMaxValue = ADC1ConvVal_CD4052[ucI][0];
		ADCSample.DATA[ucI] = 0;
		for (ucII=0;ucII<ADC_Sample_Num;ucII++)
		{						
			ADCSample.DATA[ucI] += ADC1ConvVal_CD4052[ucI][ucII];
			if( ADC1ConvVal_CD4052[ucI][ucII] < adcMinValue )
			adcMinValue = ADC1ConvVal_CD4052[ucI][ucII];		
			if( ADC1ConvVal_CD4052[ucI][ucII] > adcMaxValue )
			adcMaxValue = ADC1ConvVal_CD4052[ucI][ucII];		
		}
		ADCSample.DATA[ucI] -= adcMinValue;
		ADCSample.DATA[ucI] -= adcMaxValue;
		ADCSample.DATA[ucI] = ADCSample.DATA[ucI] / (ADC_Sample_Num - 2);
	}
}

void OptPowerConvert(unsigned short sampleADCData , u8 Index)
{
	short sTemp;
	
	if( sampleADCData < AdcChannelData.SampleRef )
	{
		sampleADCData = AdcChannelData.SampleRef + 1;
	}
	sTemp = sampleADCData - AdcChannelData.SampleRef;
	//if( sTemp >= POWEROFFSET )
	if( sTemp >= 0 )
	{	
		sTemp = sTemp - ONS3ECard.PDStage1VolOffset[Index];
		if( sTemp < 1 )
		{
			sTemp = 1;
		}
		
		if(Index == 6)
		{
			AdcChannelData.POWER[0][Index] = IntegerToLogarithm( (unsigned int)sTemp ) + ONS3ECard.PD_cal[Index][0];			
		}
		else
		{
			if(ONS3ECard.GAINStage[Index][0] == 0x01)
			AdcChannelData.POWER[0][Index] = IntegerToLogarithm( (unsigned int)sTemp ) + ONS3ECard.PD_cal[Index][ONS3ECard.GAINStage[Index][0]*2 + ONS3ECard.GAINStage[Index][2]];
			else
			AdcChannelData.POWER[0][Index] = IntegerToLogarithm( (unsigned int)sTemp ) + ONS3ECard.PD_cal[Index][ONS3ECard.GAINStage[Index][0]*2 + ONS3ECard.GAINStage[Index][1]];
		}
	}
}

//采样通道数字量转换成对应的光功率值
/*void ConvertSampleData( void )
{
	unsigned short sampleADCData;
	//short sTemp;
	char  ucI;
		
  AdcChannelData.SampleRef = 0;
	
	//ADC PORT_Power: PORT1~PORT7
	for(ucI=0; ucI<6; ucI++)
	{
		switch(ucI)
		{
			case 0:   sampleADCData = ADCSample.Type.CH3_PWR_J1;
								if(GAINSEL_CH30_STA == 1)    //SGM3157 CH30-->NO
								{
									ONS3ECard.GAINStage[ucI][0] = 0x01;
									
									if(GAINSEL_CH32_STA == 1)   //SGM3157 CH32-->NO
									{
										ONS3ECard.GAINStage[ucI][2] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH32_Set_Low;	
										}
									}
									else if(GAINSEL_CH32_STA == 0)   //SGM3157 CH32-->NC
									{
										ONS3ECard.GAINStage[ucI][2] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH32_Set_Hig;
										}
										else if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH30_Set_Low;
										}
									}									
								}
								else if(GAINSEL_CH30_STA == 0)   //SGM3157 CH30-->NC
								{
									ONS3ECard.GAINStage[ucI][0] = 0x00;
									
									if(GAINSEL_CH31_STA == 1)     //SGM3157 CH31-->NO
									{
										ONS3ECard.GAINStage[ucI][1] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH31_Set_Low;	
										}
										else if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH30_Set_Hig;
										}
									}
									else if(GAINSEL_CH31_STA == 0)     //SGM3157 CH31-->NC
									{
										ONS3ECard.GAINStage[ucI][1] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH31_Set_Hig;
										}
									}
								}			
								OptPowerConvert(sampleADCData , ucI);
			          break;
			case 1:   if(PG.SelectedPort[0] == PG.PrimaryPort[0])
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][3] - PG.OptSwitchRXIL[0][0];
								}
								else
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][5] - PG.OptSwitchRXIL[0][1];
								}
			          break;									
			case 2:   				
								if(PG.SelectedPort[0] == PG.PrimaryPort[0])
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][0] - PG.OptSwitchTXIL[0][0][0];
								}
								else
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][6] - PG.OptSwitchTXIL[0][1][0];
								}
			          break;					
			case 3:   sampleADCData = ADCSample.Type.CH1_PWR_J1;
								if(GAINSEL_CH10_STA == 1)    //SGM3157 CH10-->NO
								{
									ONS3ECard.GAINStage[ucI][0] = 0x01;
									
									if(GAINSEL_CH12_STA == 1)   //SGM3157 CH12-->NO
									{
										ONS3ECard.GAINStage[ucI][2] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH12_Set_Low;	
										}
									}
									else if(GAINSEL_CH12_STA == 0)   //SGM3157 CH12-->NC
									{
										ONS3ECard.GAINStage[ucI][2] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH12_Set_Hig;
										}
										else if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH10_Set_Low;
										}
									}									
								}
								else if(GAINSEL_CH10_STA == 0)   //SGM3157 CH10-->NC
								{
									ONS3ECard.GAINStage[ucI][0] = 0x00;
									
									if(GAINSEL_CH11_STA == 1)     //SGM3157 CH11-->NO
									{
										ONS3ECard.GAINStage[ucI][1] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH11_Set_Low;	
										}
										else if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH10_Set_Hig;
										}
									}
									else if(GAINSEL_CH11_STA == 0)     //SGM3157 CH11-->NC
									{
										ONS3ECard.GAINStage[ucI][1] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH11_Set_Hig;
										}
									}
								}				
								OptPowerConvert(sampleADCData , ucI);
								//ONS3ECard.OutputPower = AdcChannelData.POWER[0][ucI];
			          break;
			case 4:   			
								if(PG.SelectedPort[0] == PG.PrimaryPort[0])
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][6] - PG.OptSwitchTXIL[0][0][1];
								}
								else
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][0] - PG.OptSwitchTXIL[0][1][1];
								}									
			          break;	
			case 5:   sampleADCData = ADCSample.Type.CH2_PWR_J1;
								if(GAINSEL_CH20_STA == 1)    //SGM3157 CH20-->NO
								{
									ONS3ECard.GAINStage[ucI][0] = 0x01;
									
									if(GAINSEL_CH22_STA == 1)  //SGM3157 CH22-->NO
									{
										ONS3ECard.GAINStage[ucI][2] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH22_Set_Low;	
										}
									}
									else if(GAINSEL_CH22_STA == 0)   //SGM3157 CH22-->NC
									{
										ONS3ECard.GAINStage[ucI][2] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH22_Set_Hig;
										}
										else if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH20_Set_Low;
										}
									}									
								}
								else if(GAINSEL_CH20_STA == 0)   //SGM3157 CH20-->NC
								{
									ONS3ECard.GAINStage[ucI][0] = 0x00;
									
									if(GAINSEL_CH21_STA == 1)     //SGM3157 CH21-->NO
									{
										ONS3ECard.GAINStage[ucI][1] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH21_Set_Low;	
										}
										else if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH20_Set_Hig;
										}
									}
									else if(GAINSEL_CH21_STA == 0)     //SGM3157 CH21-->NC
									{
										ONS3ECard.GAINStage[ucI][1] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH21_Set_Hig;
										}
									}
								}									
								OptPowerConvert(sampleADCData , ucI);
			          break;								
			
			default:  							
			          break;
		}

		if( AdcChannelData.POWER[0][ucI] < alarmThresholds.LOSVAL[ucI] )
		{
			AdcChannelData.POWER[0][ucI] = NO_POWER;
		}		

		ONS3ECard.OutputPower = AdcChannelData.POWER[0][3];
	}
}*/

void ConvertSampleData( void )
{
	unsigned short sampleADCData;
	//short sTemp;
	char  ucI;
		
  AdcChannelData.SampleRef = 0;
	
	//ADC PORT_Power: PORT1~PORT7
	for(ucI=0; ucI<6; ucI++)
	{
		switch(ucI)
		{
			case 0:   sampleADCData = ADCSample.Type.CH3_PWR_J1;
								if(GAINSEL_CH30_STA == 1)    //SGM3157 CH30-->NO
								{
									ONS3ECard.GAINStage[ucI][0] = 0x01;
									
									if(GAINSEL_CH32_STA == 1)   //SGM3157 CH32-->NO
									{
										ONS3ECard.GAINStage[ucI][2] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH32_Set_Low;	
										}
									}
									else if(GAINSEL_CH32_STA == 0)   //SGM3157 CH32-->NC
									{
										ONS3ECard.GAINStage[ucI][2] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH32_Set_Hig;
										}
										else if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH30_Set_Low;
										}
									}									
								}
								else if(GAINSEL_CH30_STA == 0)   //SGM3157 CH30-->NC
								{
									ONS3ECard.GAINStage[ucI][0] = 0x00;
									
									if(GAINSEL_CH31_STA == 1)     //SGM3157 CH31-->NO
									{
										ONS3ECard.GAINStage[ucI][1] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH31_Set_Low;	
										}
										else if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH30_Set_Hig;
										}
									}
									else if(GAINSEL_CH31_STA == 0)     //SGM3157 CH31-->NC
									{
										ONS3ECard.GAINStage[ucI][1] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH31_Set_Hig;
										}
									}
								}			
								OptPowerConvert(sampleADCData , ucI);
			          break;
			case 1:   /*if(PG.SelectedPort[0] == PG.PrimaryPort[0])
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][3] - PG.OptSwitchRXIL[0][0];
								}
								else
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][5] - PG.OptSwitchRXIL[0][1];
								}*/
			          break;									
			case 2:   				
								/*if(PG.SelectedPort[0] == PG.PrimaryPort[0])
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][0] - PG.OptSwitchTXIL[0][0][0];
								}
								else
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][6] - PG.OptSwitchTXIL[0][1][0];
								}*/
			          break;					
			case 3:   sampleADCData = ADCSample.Type.CH1_PWR_J1;
								if(GAINSEL_CH10_STA == 1)    //SGM3157 CH10-->NO
								{
									ONS3ECard.GAINStage[ucI][0] = 0x01;
									
									if(GAINSEL_CH12_STA == 1)   //SGM3157 CH12-->NO
									{
										ONS3ECard.GAINStage[ucI][2] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH12_Set_Low;	
										}
									}
									else if(GAINSEL_CH12_STA == 0)   //SGM3157 CH12-->NC
									{
										ONS3ECard.GAINStage[ucI][2] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH12_Set_Hig;
										}
										else if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH10_Set_Low;
										}
									}									
								}
								else if(GAINSEL_CH10_STA == 0)   //SGM3157 CH10-->NC
								{
									ONS3ECard.GAINStage[ucI][0] = 0x00;
									
									if(GAINSEL_CH11_STA == 1)     //SGM3157 CH11-->NO
									{
										ONS3ECard.GAINStage[ucI][1] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH11_Set_Low;	
										}
										else if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH10_Set_Hig;
										}
									}
									else if(GAINSEL_CH11_STA == 0)     //SGM3157 CH11-->NC
									{
										ONS3ECard.GAINStage[ucI][1] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH11_Set_Hig;
										}
									}
								}				
								OptPowerConvert(sampleADCData , ucI);
								//ONS3ECard.OutputPower = AdcChannelData.POWER[0][ucI];								
			          break;
			case 4:   			
								/*if(PG.SelectedPort[0] == PG.PrimaryPort[0])
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][6] - PG.OptSwitchTXIL[0][0][1];
								}
								else
								{
									AdcChannelData.POWER[0][ucI] = AdcChannelData.POWER[0][0] - PG.OptSwitchTXIL[0][1][1];
								}*/									
			          break;	
			case 5:   sampleADCData = ADCSample.Type.CH2_PWR_J1;
								if(GAINSEL_CH20_STA == 1)    //SGM3157 CH20-->NO
								{
									ONS3ECard.GAINStage[ucI][0] = 0x01;
									
									if(GAINSEL_CH22_STA == 1)  //SGM3157 CH22-->NO
									{
										ONS3ECard.GAINStage[ucI][2] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH22_Set_Low;	
										}
									}
									else if(GAINSEL_CH22_STA == 0)   //SGM3157 CH22-->NC
									{
										ONS3ECard.GAINStage[ucI][2] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH22_Set_Hig;
										}
										else if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH20_Set_Low;
										}
									}									
								}
								else if(GAINSEL_CH20_STA == 0)   //SGM3157 CH20-->NC
								{
									ONS3ECard.GAINStage[ucI][0] = 0x00;
									
									if(GAINSEL_CH21_STA == 1)     //SGM3157 CH21-->NO
									{
										ONS3ECard.GAINStage[ucI][1] = 0x01;
										
										if(sampleADCData > MAXCode)
										{
											GAIN_SEL_CH21_Set_Low;	
										}
										else if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH20_Set_Hig;
										}
									}
									else if(GAINSEL_CH21_STA == 0)     //SGM3157 CH21-->NC
									{
										ONS3ECard.GAINStage[ucI][1] = 0x00;
										
										if(sampleADCData < MINCode)
										{
											GAIN_SEL_CH21_Set_Hig;
										}
									}
								}									
								OptPowerConvert(sampleADCData , ucI);
			          break;								
			
			default:  							
			          break;
		}

		if( AdcChannelData.POWER[0][ucI] < alarmThresholds.LOSVAL[ucI] )
		{
			AdcChannelData.POWER[0][ucI] = NO_POWER;
		}		

		//ONS3ECard.OutputPower = AdcChannelData.POWER[0][3];
	}
	
	ONS3ECard.OutputPower = AdcChannelData.POWER[0][3]; //输出功率与R1功率一致
	
	//RX光功率
	if(PG.SelectedPort[0] == PG.PrimaryPort[0])
	{
		AdcChannelData.POWER[0][1] = AdcChannelData.POWER[0][3] - PG.OptSwitchRXIL[0][0];		//在主路RX与R1功率一致
	}
	else
	{
		AdcChannelData.POWER[0][1] = AdcChannelData.POWER[0][5] - PG.OptSwitchRXIL[0][1];		//在备路RX与R2功率一致
	}
	
	//T1光功率
	if(PG.SelectedPort[0] == PG.PrimaryPort[0])
	{
		AdcChannelData.POWER[0][2] = AdcChannelData.POWER[0][0] - PG.OptSwitchTXIL[0][0][0];
	}
	else
	{
		AdcChannelData.POWER[0][2] = AdcChannelData.POWER[0][6] - PG.OptSwitchTXIL[0][1][0];
	}
	
	//T2光功率
	if(PG.SelectedPort[0] == PG.PrimaryPort[0])
	{
		AdcChannelData.POWER[0][4] = AdcChannelData.POWER[0][6] - PG.OptSwitchTXIL[0][0][1];
	}
	else
	{
		AdcChannelData.POWER[0][4] = AdcChannelData.POWER[0][0] - PG.OptSwitchTXIL[0][1][1];
	}	
}

void ConvertSampleData_Pump( void )
{
	unsigned short sampleADCData;
	//short sTemp;
	//char  ucI;
	float temp;	
		
  AdcChannelData.SampleRef = 0;
	
	//ADC SFP_TX_Power: 
	sampleADCData = ADCSample.Type.CH4_PWR_J1;
	OptPowerConvert(sampleADCData , 6);
	if( AdcChannelData.POWER[0][6] < alarmThresholds.LOSVAL[6] )
	{
		AdcChannelData.POWER[0][6] = NO_POWER;
	}			

	//ADC_Pump1_Tec
	sampleADCData = ADCSample.Type.P1_TEC_J1*factor;
	
	temp = (float)sampleADCData - (float)Vref_Tec/2;
	temp = temp / 0.525f;									
	AdcChannelData.PumpTec[0] = (short)(10 * temp );


	//ADC_PUMP1_Ld
	sampleADCData = ADCSample.Type.P1_ILD_J1*factor;
	if( sampleADCData > 10 )
	{	
		AdcChannelData.PumpILD[0] = (float)sampleADCData * 10000 / ONS3ECard.K1;
	}
	else
	{
		AdcChannelData.PumpILD[0] = 0;
	}
	
	//ADC_PUMP_Ts
	sampleADCData = ADCSample.Type.P1_TEMP_J1*factor;
	if( sampleADCData  < Vref_Tec/2 )//>25C Vref/2=1250
	{
		temp = ( Vref_Tec/2 - sampleADCData ) * 0.4;
		AdcChannelData.PumpTs[0] =  250 + temp;
		
		AdcChannelData.PumpTs[0] = AdcChannelData.PumpTs[0] - ONS3ECard.pump1TempOffset;
	}
	else
	{
		temp = ( sampleADCData - Vref_Tec/2 ) * 0.6;
		AdcChannelData.PumpTs[0] = 250 - temp ;
		
		AdcChannelData.PumpTs[0] = AdcChannelData.PumpTs[0] + ONS3ECard.pump1TempOffset;
	}
	
	//ADC_Pump_Op
	sampleADCData = ADCSample.Type.P1_OP_J1 * factor;
	if( sampleADCData > 5 )
	{					
		AdcChannelData.PumpOp[0] +=(float)sampleADCData /( ONS3ECard.Pump1K /1000.0);
		AdcChannelData.PumpOp[0] = AdcChannelData.PumpOp[0] / 2;
 	}
	else
  {   
		AdcChannelData.PumpOp[0] = 0;				
	}
}

void CardTempAlarm( void )
{
	/*模块高温报警处理*/
	if( ONS3ECard.Temp[0] >= alarmThresholds.CardMTH[0] )
	{
		BitSet( &CardStatusInfo.alarmSTA.general,Card_STA_MTH );
		ONS3ECard.ALRM_TEMP_H = ALRM_ON;
		ONS3ECard.ALRMInfo[0][0] |= 0x20;
	}
	else if( ONS3ECard.Temp[0] < ( alarmThresholds.CardMTH[0] - alarmHysteresis.CardMTH ) )
	{
		BitClear( &CardStatusInfo.alarmSTA.general,Card_STA_MTH );
		ONS3ECard.ALRM_TEMP_H = ALRM_OFF;
		ONS3ECard.ALRMInfo[0][0] &= 0xDF;
		
	 /*模块低温报警处理*/
	 if( ONS3ECard.Temp[0] <= alarmThresholds.CardMTL[0] )
	 {
		 BitSet( &CardStatusInfo.alarmSTA.general,Card_STA_MTL );
		 ONS3ECard.ALRM_TEMP_L = ALRM_ON;
		 ONS3ECard.ALRMInfo[0][0] |= 0x10;
	 }
	 else if( ONS3ECard.Temp[0] > ( alarmThresholds.CardMTL[0] + alarmHysteresis.CardMTL ) )
	 {
		 BitClear( &CardStatusInfo.alarmSTA.general,Card_STA_MTL );
		 ONS3ECard.ALRM_TEMP_L = ALRM_OFF;
		 ONS3ECard.ALRMInfo[0][0] &= 0xEF;
	 }
	}
}

void SFPLOSAlarm(void)
{
	if(SFP1_LOS == 1)
	{
		ucLOSAlarmCount++;
		if(ucLOSAlarmCount > 20)
		{													
			ucLOSAlarmCount = 20;
			
			if(ONS3ECard.SFFModule.PortENABLE[0] == TRUE)
			ONS3ECard.ALRMInfo[0][6]|=1<<0;
			else
			ONS3ECard.ALRMInfo[0][6]&=~(1<<0);	
		}
	}
	else
	{
		ucLOSAlarmCount = 0;
		ONS3ECard.ALRMInfo[0][6]&=~(1<<0);
	}
}


void CardPDLosAlarm(void)
{
	char ucI;
	
	for(ucI=0; ucI<7; ucI++)
	{
		if(AdcChannelData.POWER[0][ucI] < alarmThresholds.LOS[0][ucI] )
		{
			ucInputPowerAlarmCount[ucI]++;
			if(ucInputPowerAlarmCount[ucI] > 20)
			{
				ucInputPowerAlarmCount[ucI] = 20;
				
				BitSet( &SFFStatusInfo.alarmSTA.LOS_PD,ucI );		
			}
		}
		else if( AdcChannelData.POWER[0][ucI] > ( alarmThresholds.LOS[0][ucI] + alarmHysteresis.LOS[ucI] ) )
		{
			ucInputPowerAlarmCount[ucI] = 0;			
			
			BitClear( &SFFStatusInfo.alarmSTA.LOS_PD,ucI );
		}
	}
}


void RamanSignalPowerCal(void)
{
	if( ONS3ECard.pumpStatus == PUMP_OFF )
	{
		ONS3ECard.ramanIn = ONS3ECard.OutputPower + ONS3ECard.ramanRIL;
		ONS3ECard.signalPower[0] = ONS3ECard.ramanIn;
		ONS3ECard.offSignalPower = ONS3ECard.ramanIn;
		ONS3ECard.ramanGain = 0;
		ONS3ECard.ramanASEPower = 0;
		
		RamanPump1BallenceFlag = FALSE;
	}
	else
	{
		if ( ONS3ECard.OpMode[0] == GAIN_MODE )
		{
			ONS3ECard.ramanTargetPower = getRanmanPOWER( ONS3ECard.ramanGainSet[0] );
			if (RamanPump1BallenceFlag == TRUE )
			{
				//ONS3ECard.ramanASEPower = getThreeDimensionalTableData(RAMANASE_IN_TBL,10,8,ONS3ECard.ramanGainSet[0],ONS3ECard.linepowerAlrmThr );
				ONS3ECard.ramanASEPower = getThreeDimensionalTableData(RAMANASE_IN_TBL,ONS3ECard.RamanGAINNum,ONS3ECard.InputPowerNum,ONS3ECard.ramanGainSet[0],ONS3ECard.OutputPower - ONS3ECard.ramanGainSet[0] );
				//ONS3ECard.ramanTargetPower = getRanmanPOWER( ONS3ECard.ramanGainSet[0] );
				ONS3ECard.signalPower[0] = ONS3ECard.OutputPower - ONS3ECard.ramanGainSet[0] - ONS3ECard.ramanASEPower;		//折算输入光包含IL插损
				ONS3ECard.ramanIn = ONS3ECard.signalPower[0];
				ONS3ECard.ramanGain = ONS3ECard.ramanGainSet[0];
			}
		}
		else if ( ONS3ECard.OpMode[0] == POWER_MODE )
		{
			ONS3ECard.ramanGain = getRanmanGAIN( ONS3ECard.wave1Powerset[0] );
			if (RamanPump1BallenceFlag == TRUE )
			{
				ONS3ECard.ramanASEPower = getThreeDimensionalTableData(RAMANASE_IN_TBL,ONS3ECard.RamanGAINNum,ONS3ECard.InputPowerNum,ONS3ECard.ramanGain,ONS3ECard.OutputPower - ONS3ECard.ramanGain );
				ONS3ECard.signalPower[0] = ONS3ECard.OutputPower - ONS3ECard.ramanGain - ONS3ECard.ramanASEPower;		//折算输入光包含IL插损
				ONS3ECard.ramanIn = ONS3ECard.signalPower[0];
			}
		}
		/*else
		{
			ONS3ECard.signalPower[0] = ONS3ECard.OutputPower;
			//return;
		}*/
	}
}
#if 0
void AmpSignpowerAlarm(void)
{	
	if( ONS3ECard.signalPower[0] < alarmThresholds.Signal[0] )	
	{
		ONS3ECard.loopNum = 0;
		ucInputPowerAlarmCount++;
		if(ucInputPowerAlarmCount>5)
		{
			Input_PWRLOW_ALRM_Set_Hig;
			alarmInfo.Signal = ENABLE;
		}
		BitSet( &alarmStatusInfo.alarmSTA.general,ALARM_SIG );
	}
	
	else if( ONS3ECard.signalPower[0] >= alarmThresholds.Signal[0] + alarmHysteresis.Signal )
	{
		BitClear( &alarmStatusInfo.alarmSTA.general,ALARM_SIG );
		Input_PWRLOW_ALRM_Set_Low;
		alarmInfo.Signal = DISABLE;
		ucInputPowerAlarmCount = 0;	
	}
}
#endif

void AmpLowpowerAlarm(void)
{
	/*if( PG.CURCH_RX[0] == 0x00 )
	{
		ONS3ECard.linepowerAlrmThr = alarmThresholds.Low[0];
		ONS3ECard.linepowerAlrmHys = alarmHysteresis.Low;
	}
	else if( PG.CURCH_RX[0] == 0x01 )
	{						
		ONS3ECard.linepowerAlrmThr = alarmThresholds.Signal[0];
		ONS3ECard.linepowerAlrmHys = alarmHysteresis.Signal;
	}
	else
	{
		return;
	}*/		
	
	if( PG.CURCH_RX[0] == 0x00 )
	{
		ONS3ECard.linepowerAlrmThr = alarmThresholds.Signal[0];
		ONS3ECard.linepowerAlrmHys = alarmHysteresis.Signal;
	}
	else if( PG.CURCH_RX[0] == 0x01 )
	{						
		ONS3ECard.linepowerAlrmThr = alarmThresholds.Low[0];
		ONS3ECard.linepowerAlrmHys = alarmHysteresis.Low;
	}
	else
	{
		return;
	}		
	
	if( ONS3ECard.signalPower[0] < ONS3ECard.linepowerAlrmThr )	
	{
		ONS3ECard.loopNum = 0;
		ucInputLowPowerAlarmCount++;
		if (ucInputLowPowerAlarmCount>5)
		{
			alarmInfo.Low = ENABLE;
		}
		BitSet( &alarmStatusInfo.alarmSTA.general,ALARM_LOW );
	}
	else if( ONS3ECard.signalPower[0] >= ONS3ECard.linepowerAlrmThr + ONS3ECard.linepowerAlrmHys )
	{
		BitClear( &alarmStatusInfo.alarmSTA.general,ALARM_LOW );
		alarmInfo.Low = DISABLE;
		//ucInputPowerAlarmCount = 0;	
	}
}

void PumpCurrentAlarm(void)
{
	float temp1;
	float Min1;
	
	Min1 = ONS3ECard.PCT1 * (alarmThresholds.ILD - alarmHysteresis.ILD) / 100.0;

	
	if( ONS3ECard.pumpNum =='1')
	{
		temp1 = (float)ONS3ECard.PCT1 * alarmThresholds.ILD / 100.0f;
		if( AdcChannelData.PumpILD[0] > temp1 )
		{
			ucPumpCurrentAlarmCount++;
			if( ucPumpCurrentAlarmCount >= 50)
			{
				BitSet( &alarmStatusInfo.alarmSTA.general,ALARM_ILD );
				
				ONS3ECard.ALRMInfo[0][0] |= 0x08;
			}
		}
		else if(AdcChannelData.PumpILD[0] < Min1 )
		{
			ucPumpCurrentAlarmCount = 0;
			BitClear( &alarmStatusInfo.alarmSTA.general,ALARM_ILD );
			ONS3ECard.ALRMInfo[0][0] &= 0xF7;
		}
	}
}

void PumpTempAlarm( void )
{
	short MAXAlrmOn,MINAlrmOn;
	short MAXAlrmOff,MINAlrmOff;
	
	MAXAlrmOn = PUMP_TSP + alarmThresholds.TMP;
	MINAlrmOn = PUMP_TSP - alarmThresholds.TMP ;
	
	if( ONS3ECard.pumpNum =='1')
	{
		if( ( AdcChannelData.PumpTs[0] < MINAlrmOn ) || ( AdcChannelData.PumpTs[0] > MAXAlrmOn ) ) 
		{
			ucPumpTmpAlarmCount++;
			if( ucPumpTmpAlarmCount > 50 )
			{
				ucPumpTmpAlarmCount = 50;
				BitSet( &alarmStatusInfo.alarmSTA.general,ALARM_TMP );
				alarmInfo.TMP = ENABLE;
				
				ONS3ECard.ALRMInfo[0][0] |= 0x04;
			}
		}
		else
		{
			MAXAlrmOff = PUMP_TSP + alarmThresholds.TMP - alarmHysteresis.TMP;
			MINAlrmOff = PUMP_TSP - alarmThresholds.TMP + alarmHysteresis.TMP;
			if( ( AdcChannelData.PumpTs[0] >= MINAlrmOff )&& ( AdcChannelData.PumpTs[0] <= MAXAlrmOff ) )
			{
				ucPumpTmpAlarmCount = 0;
				BitClear( &alarmStatusInfo.alarmSTA.general,ALARM_TMP );
				alarmInfo.TMP = DISABLE;
				
				ONS3ECard.ALRMInfo[0][0] &= 0xFB;
			}
		}	
	}
}


void CardAbnormalAlarm(void)
{
	if(	 ( (SFFStatusInfo.alarmSTA.LOS_PD&0x3F) == 0x00 )   \
		&& ( (SFFStatusInfo.alarmSTA.general&0x07) == 0x00 ) 	\
		&& ( ONS3ECard.ALRM_TEMP_H == ALRM_OFF ) 			   			\
		&& ( ONS3ECard.ALRM_TEMP_L == ALRM_OFF )
	)	
	{
		ONS3ECard.STA[0] = 0x00;
		LED_ALRM_Set_Low;
		
		ONS3ECard.LEDState.Fault[0] = LED_STATA_OFF;
	}
	else
	{
		ONS3ECard.STA[0] = 0x01;
		LED_ALRM_Set_Hig;
		
		ONS3ECard.LEDState.Fault[0] = LED_STATA_RedAlways;
	}
}

void AlarmSample(void)
{		
	CardTempAlarm();
	SFPLOSAlarm();
	CardPDLosAlarm();
	AmpLowpowerAlarm();
	PumpCurrentAlarm();
	PumpTempAlarm();
	CardAbnormalAlarm();
}


void PGLineBackSwitch( void )
{
	if( ONS3ECard.signalPower[0] > ( PG.ABSTHR[0][0] + alarmHysteresis.LOS[3] ) )
	{
		MFSWSWToHost();
		PG.SelectedPort[0] = PG.PrimaryPort[0];
		PG.ProtectionDirection[0] = ToPrimary;
		PG.SwitchReason[0] = SWCAUSE_WTR;
		PG.SwitchCMDTypePre[0] = PG.SwitchCMDType[0];
					
		WriteEEPRom( ADDR_PG_ProtectionDir, PG.ProtectionDirection[0] );
		WriteEEPRom( ADDR_PG_SelectPort, PG.SelectedPort[0] );
		ONS3ECard.EEPROMBuff[ADDR_PG_ProtectionDir] = PG.ProtectionDirection[0];
		ONS3ECard.EEPROMBuff[ADDR_PG_SelectPort] = PG.SelectedPort[0];
		EEPROMdataCheck();
		
		ONS3ECard.BACKSWStartFlag = 0x00;
		TIM2BACK_SW_Count[0] = 0;
		ONS3ECard.BACKShakeCheck = 0;
	}
}

void MFSWSWToHost( void )
{
	MFSW_SWITCH_HOST_PG_TX();
	MFSW_SWITCH_HOST_PG_RX();
	
	PG.SWCount[0]++;
}

void MFSWSWToBackup( void )
{
	MFSW_SWITCH_BACKUP_PG_TX();
	MFSW_SWITCH_BACKUP_PG_RX();
	
	PG.SWCount[0]++;
}

void SWMODERecoverCheck( void )
{
	if(PG.SWMODERecover[0] == ENABLE && PG.SwitchCMDType[0] != SWCAUSE_SF)
	{
		//TIM2SWMODEReover_Count[0] = 0;
		ONS3ECard.SWMODERecoverStartFlag = TRUE;
	}
	else
	{
		ONS3ECard.SWMODERecoverStartFlag = FALSE;
	}
}

/*
void Request_sync_switch(u8 SetPort)  //SetPort用于倒换方向
{
	SHORTVAR	TempData_s;
	
	ONS3ECard.SYNCSWFlag = TRUE;
	
	ONS3ECard.SetObject = OLPSFSYNCSWSet;
	ONS3ECard.SetPort = SetPort;   //设置端口号
	ONS3ECard.SetDataLen = 0x02;
	
	ONS3ECard.SetDataBuff[0] = CMD_Set;
	ONS3ECard.SetDataBuff[1] = ONS3ECard.Addr[1];
	ONS3ECard.SetDataBuff[2] = (ONS3ECard.SetObject>>8)&0xff;
	ONS3ECard.SetDataBuff[3] = ONS3ECard.SetObject&0xff;
	ONS3ECard.SetDataBuff[4] = SetPort;
	ONS3ECard.SetDataBuff[5] = ONS3ECard.SetDataLen;
	
	TempData_s.sWord = AdcChannelData.POWER[0][0];	
	ONS3ECard.SetDataBuff[FixedDataLen + 0] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 1] = TempData_s.ucByte[0];
	
	LSdataSend(&ONS3ECard.SetDataBuff[0],FixedDataLen + ONS3ECard.SetDataLen);
	//delay_ms(1);
	
	//printf("cur local r1pwr: %3.2f remode txpwr: %3.2f\r\n",(float)AdcChannelData.POWER[0][3]/100,(float)AdcChannelData.POWER[1][0]/100);
	//printf("Request_sync_switch:\r\n\r\n");
	
	//Delay_100us(5);

	ONS3ECard.SetCMDSendCount = 0;
	ONS3ECard.SetType = USARTSET;
	ONS3ECard.SetObjectFail = ONS3ECard.SetObject;
	
	//OSTaskResume( SET_TASK_PRIO );  							//恢复设置任务
	//OSTaskSuspend( RemoteInfoGet_TASK_PRIO );			//挂起Remote信息获取任务
}

void Respond_sync_switch(void)
{
	SHORTVAR	TempData_s;

	ONS3ECard.SetObject = OLPSFSYNCSWSet;
	ONS3ECard.SetDataLen = 0x02;
	
	ONS3ECard.SetDataBuff[0] = 0x81;
	ONS3ECard.SetDataBuff[1] = ONS3ECard.Addr[1];
	ONS3ECard.SetDataBuff[2] = (ONS3ECard.SetObject>>8)&0xff;
	ONS3ECard.SetDataBuff[3] = ONS3ECard.SetObject&0xff;
	ONS3ECard.SetDataBuff[4] = PG.ProtectionDirTemp;
	ONS3ECard.SetDataBuff[5] = ONS3ECard.SetDataLen;
	
	TempData_s.sWord = AdcChannelData.POWER[0][0];	
	ONS3ECard.SetDataBuff[FixedDataLen + 0] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 1] = TempData_s.ucByte[0];
	
	//printf("cur local txpwr: %3.2f\r\n",(float)AdcChannelData.POWER[0][0]/100);
	//printf("Respond_sync_switch:\r\n\r\n");
	
	if( (AdcChannelData.POWER[0][0] > ( alarmThresholds.LOS[0][0] + alarmHysteresis.LOS[0] )) && (AdcChannelData.POWER[1][0] > ( alarmThresholds.LOS[1][0] + alarmHysteresis.LOS[0] )))
	{
		ONS3ECard.SYNCSWFlag = TRUE;
		
		LSdataSend(&ONS3ECard.SetDataBuff[0],FixedDataLen + ONS3ECard.SetDataLen);
		delay_ms(1);
		//Delay_100us(10);
		
		ProtectionDirectionProcess(1);	
		
		ONS3ECard.SYNCSWFlag = FALSE;
		
		PG.SwitchReason[0] = SWCAUSE_SF;
	}
	else
	{
		LSdataSend(&ONS3ECard.SetDataBuff[0],FixedDataLen + ONS3ECard.SetDataLen);
		
		//ONS3ECard.SYNCSWFlag = FALSE;
	}
}*/

void request_remote_cur_OptPwr(void)
{
	SHORTVAR	TempData_s;
	//u8 ucI;
	
	ONS3ECard.SetObject = OLPRequestRemotePwr;
	ONS3ECard.SetDataLen = 0x10;
	
	ONS3ECard.SetDataBuff[0] = CMD_Get;
	ONS3ECard.SetDataBuff[1] = ONS3ECard.Addr[1];
	ONS3ECard.SetDataBuff[2] = (ONS3ECard.SetObject>>8)&0xff;
	ONS3ECard.SetDataBuff[3] = ONS3ECard.SetObject&0xff;
	ONS3ECard.SetDataBuff[4] = CARDSELF;
	ONS3ECard.SetDataBuff[5] = ONS3ECard.SetDataLen;
	
	TempData_s.sWord = AdcChannelData.POWER[0][0];	
	ONS3ECard.SetDataBuff[FixedDataLen + 0] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 1] = TempData_s.ucByte[0];
	
	TempData_s.sWord = AdcChannelData.POWER[0][3];	
	ONS3ECard.SetDataBuff[FixedDataLen + 2] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 3] = TempData_s.ucByte[0];
	
	TempData_s.sWord = AdcChannelData.POWER[0][5];	
	ONS3ECard.SetDataBuff[FixedDataLen + 4] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 5] = TempData_s.ucByte[0];
	
	TempData_s.sWord = ONS3ECard.signalPower[0];	
	ONS3ECard.SetDataBuff[FixedDataLen + 6] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 7] = TempData_s.ucByte[0];
	
	TempData_s.sWord = PG.ABSTHR[0][0];
	ONS3ECard.SetDataBuff[FixedDataLen + 8] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 9] = TempData_s.ucByte[0];
	
	TempData_s.sWord = PG.ABSTHR[0][1];
	ONS3ECard.SetDataBuff[FixedDataLen + 10] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 11] = TempData_s.ucByte[0];
	
	TempData_s.sWord = PG.OFFSETTHR[0][0];
	ONS3ECard.SetDataBuff[FixedDataLen + 12] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 13] = TempData_s.ucByte[0];
	
	TempData_s.sWord = PG.OFFSETTHR[0][1];
	ONS3ECard.SetDataBuff[FixedDataLen + 14] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 15] = TempData_s.ucByte[0];

	LSdataSend(&ONS3ECard.SetDataBuff[0],FixedDataLen + ONS3ECard.SetDataLen);
	
	/*printf("cur local r1pwr: %3.2f remode txpwr: %3.2f\r\n",(float)AdcChannelData.POWER[0][3]/100,(float)AdcChannelData.POWER[1][0]/100);
	printf("remote_OptPwr_Request_count: %d\r\n",ONS3ECard.remote_OptPwr_Request_count);
	printf("request_remote_cur_OptPwr:\r\n");
	for(ucI=0;ucI<(FixedDataLen + ONS3ECard.SetDataLen);ucI++)
	printf("%x  ",ONS3ECard.SetDataBuff[ucI]);
	printf("\r\n\r\n");*/
	
}

void respond_remote_cur_OptPwr(void)
{
	SHORTVAR	TempData_s;
	//u8 ucI;
	
	ONS3ECard.SetObject = OLPRequestRemotePwr;
	ONS3ECard.SetDataLen = 0x11;
	
	ONS3ECard.SetDataBuff[0] = CMD_Get;
	ONS3ECard.SetDataBuff[1] = ONS3ECard.Addr[1];
	ONS3ECard.SetDataBuff[2] = (ONS3ECard.SetObject>>8)&0xff;
	ONS3ECard.SetDataBuff[3] = ONS3ECard.SetObject&0xff;
	ONS3ECard.SetDataBuff[4] = CARDSELF;
	ONS3ECard.SetDataBuff[5] = ONS3ECard.SetDataLen;
	
	TempData_s.sWord = AdcChannelData.POWER[0][0];	
	ONS3ECard.SetDataBuff[FixedDataLen + 0] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 1] = TempData_s.ucByte[0];
	
	TempData_s.sWord = AdcChannelData.POWER[0][3];	
	ONS3ECard.SetDataBuff[FixedDataLen + 2] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 3] = TempData_s.ucByte[0];
	
	TempData_s.sWord = AdcChannelData.POWER[0][5];	
	ONS3ECard.SetDataBuff[FixedDataLen + 4] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 5] = TempData_s.ucByte[0];
	
	TempData_s.sWord = ONS3ECard.signalPower[0];	
	ONS3ECard.SetDataBuff[FixedDataLen + 6] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 7] = TempData_s.ucByte[0];
	
	ONS3ECard.SetDataBuff[FixedDataLen + 8] = ONS3ECard.remote_OptPwr_Respond_count;
	
	TempData_s.sWord = PG.ABSTHR[0][0];
	ONS3ECard.SetDataBuff[FixedDataLen + 9] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 10] = TempData_s.ucByte[0];
	
	TempData_s.sWord = PG.ABSTHR[0][1];
	ONS3ECard.SetDataBuff[FixedDataLen + 11] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 12] = TempData_s.ucByte[0];
	
	TempData_s.sWord = PG.OFFSETTHR[0][0];
	ONS3ECard.SetDataBuff[FixedDataLen + 13] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 14] = TempData_s.ucByte[0];
	
	TempData_s.sWord = PG.OFFSETTHR[0][1];
	ONS3ECard.SetDataBuff[FixedDataLen + 15] = TempData_s.ucByte[1];
	ONS3ECard.SetDataBuff[FixedDataLen + 16] = TempData_s.ucByte[0];

	/*printf("cur local r1pwr: %3.2f remode txpwr: %3.2f\r\n",(float)AdcChannelData.POWER[0][3]/100,(float)AdcChannelData.POWER[1][0]/100);
	printf("remote_OptPwr_Respond_count: %d\r\n",ONS3ECard.remote_OptPwr_Respond_count);
	printf("respond_remote_cur_OptPwr:\r\n");
	for(ucI=0;ucI<(FixedDataLen + ONS3ECard.SetDataLen);ucI++)
	printf("%x  ",ONS3ECard.SetDataBuff[ucI]);
	printf("\r\n\r\n");*/
	
	LSdataSend(&ONS3ECard.SetDataBuff[0],FixedDataLen + ONS3ECard.SetDataLen);
}

/*void PGOLPSYNCSwitch( void )
{
	if(PG.AUTOSW_TYPE[0] == AUTOSWTYPE_ABS)
	{
		if( (AdcChannelData.POWER[0][0] > ( alarmThresholds.LOS[0][0] + alarmHysteresis.LOS[0] )) && (AdcChannelData.POWER[1][0] > ( alarmThresholds.LOS[1][0] + alarmHysteresis.LOS[0] )))	
		{ 
			//当前端口处在备用端口
			if(PG.SelectedPort[0] == PG.SecondaryPort[0])
			{
				if( ((AdcChannelData.POWER[0][3] > ( PG.ABSTHR[0][0] + alarmHysteresis.LOS[3] )) && (AdcChannelData.POWER[0][5] < PG.ABSTHR[0][1])) ||
						((AdcChannelData.POWER[1][3] > ( PG.ABSTHR[1][0] + alarmHysteresis.LOS[3] )) && (AdcChannelData.POWER[1][5] < PG.ABSTHR[1][1]))
					)
				{
					if(PG.HoldOFF[0] == 0)
					{
						MFSWSWToHost();
						PG.SelectedPort[0] = PG.PrimaryPort[0];
						PG.ProtectionDirection[0] = ToPrimary;
						ONS3ECard.MFSWUpdateFlag = 1;	
						ONS3ECard.HOLDOFSWFlag = FALSE;
						PG.SwitchReason[0] = SWCAUSE_SF;

					}
					else
					{
						ONS3ECard.HOLDOFSWFlag = TRUE;
						PG.ProtectionDirection[0] = ToPrimary;
					}
				}
			}
			else 
			{
				if( ((AdcChannelData.POWER[0][5] > ( PG.ABSTHR[0][1] + alarmHysteresis.LOS[5] )) && (AdcChannelData.POWER[0][3] < PG.ABSTHR[0][0])) ||
						((AdcChannelData.POWER[1][5] > ( PG.ABSTHR[1][1] + alarmHysteresis.LOS[5] )) && (AdcChannelData.POWER[1][3] < PG.ABSTHR[1][0]))
					)
				{
					if(PG.HoldOFF[0] == 0)
					{
						MFSWSWToBackup();
						PG.SelectedPort[0] = PG.SecondaryPort[0];
						PG.ProtectionDirection[0] = ToSecondary;
						ONS3ECard.MFSWUpdateFlag = 1;	
						ONS3ECard.HOLDOFSWFlag = FALSE;	
						PG.SwitchReason[0] = SWCAUSE_SF;
					}	
					else
					{
						ONS3ECard.HOLDOFSWFlag = TRUE;
						PG.ProtectionDirection[0] = ToSecondary;
					}						
				}
			}
		}
	}
	else if(PG.AUTOSW_TYPE[0] == AUTOSWTYPE_OFFSET)
	{
		if(PG.SelectedPort[0] == PG.SecondaryPort[0])
		{
			if( (AdcChannelData.POWER[0][3] > ( AdcChannelData.POWER[0][5] + PG.OFFSETTHR[0][0] )) ||
					(AdcChannelData.POWER[1][3] > ( AdcChannelData.POWER[1][5] + PG.OFFSETTHR[1][0] ))
				)
			{
				if(PG.HoldOFF[0] == 0)
				{
					MFSWSWToHost();
					PG.SelectedPort[0] = PG.PrimaryPort[0];
					PG.ProtectionDirection[0] = ToPrimary;
					ONS3ECard.MFSWUpdateFlag = 1;	
					ONS3ECard.HOLDOFSWFlag = FALSE;
					PG.SwitchReason[0] = SWCAUSE_SF;
				}
				else
				{
					ONS3ECard.HOLDOFSWFlag = TRUE;
					PG.ProtectionDirection[0] = ToPrimary;
				}
			}
		}
		else 
		{
			if( (AdcChannelData.POWER[0][5] > ( AdcChannelData.POWER[0][3] + PG.OFFSETTHR[0][1] )) ||
					(AdcChannelData.POWER[1][5] > ( AdcChannelData.POWER[1][3] + PG.OFFSETTHR[1][1] ))
				)
			{
				if(PG.HoldOFF[0] == 0)
				{
					MFSWSWToBackup();
					PG.SelectedPort[0] = PG.SecondaryPort[0];
					PG.ProtectionDirection[0] = ToSecondary;
					ONS3ECard.MFSWUpdateFlag = 1;	
					ONS3ECard.HOLDOFSWFlag = FALSE;	
					PG.SwitchReason[0] = SWCAUSE_SF;
				}	
				else
				{
					ONS3ECard.HOLDOFSWFlag = TRUE;
					PG.ProtectionDirection[0] = ToSecondary;
				}				
			}
		}
	}
}*/

void PGOLPLineAutoSwitch( void )
{
	if(PG.SwitchCMDType[0] == SWCAUSE_SF)
	{
		if(PG.AUTOSW_TYPE[0] == AUTOSWTYPE_ABS)
		{
			if( (AdcChannelData.POWER[0][0] > ( alarmThresholds.LOS[0][0] + alarmHysteresis.LOS[0] )) && (AdcChannelData.POWER[1][0] > ( alarmThresholds.LOS[1][0] + alarmHysteresis.LOS[0] )))	
			{
				if(ONS3ECard.signalPower[0] < PG.ABSTHR[0][0] || AdcChannelData.POWER[0][5] < PG.ABSTHR[0][1] ||
					 ONS3ECard.signalPower[1] < PG.ABSTHR[1][0] || AdcChannelData.POWER[1][5] < PG.ABSTHR[1][1]
					)
				{ 
					if(ONS3ECard.signalPower[0] < PG.ABSTHR[0][0] || AdcChannelData.POWER[0][5] < PG.ABSTHR[0][1])
					ONS3ECard.CanBackSwFlag[0] = FALSE;
					
					//当前端口处在备用端口
					if(PG.SelectedPort[0] == PG.SecondaryPort[0])
					{
						if( ((ONS3ECard.signalPower[0] > ( PG.ABSTHR[0][0] + alarmHysteresis.LOS[3] )) && (AdcChannelData.POWER[0][5] < PG.ABSTHR[0][1])) ||
								((ONS3ECard.signalPower[1] > ( PG.ABSTHR[1][0] + alarmHysteresis.LOS[3] )) && (AdcChannelData.POWER[1][5] < PG.ABSTHR[1][1]))
							)
						{
							if(PG.HoldOFF[0] == 0)
							{
								MFSWSWToHost();
								PG.SelectedPort[0] = PG.PrimaryPort[0];
								PG.ProtectionDirection[0] = ToPrimary;
								ONS3ECard.MFSWUpdateFlag = 1;	
								ONS3ECard.HOLDOFSWFlag = FALSE;
								PG.SwitchReason[0] = SWCAUSE_SF;

							}
							else
							{
								ONS3ECard.HOLDOFSWFlag = TRUE;
								PG.ProtectionDirection[0] = ToPrimary;
							}
						}
					}
					else 
					{
						if( ((AdcChannelData.POWER[0][5] > ( PG.ABSTHR[0][1] + alarmHysteresis.LOS[5] )) && (ONS3ECard.signalPower[0] < PG.ABSTHR[0][0])) ||
								((AdcChannelData.POWER[1][5] > ( PG.ABSTHR[1][1] + alarmHysteresis.LOS[5] )) && (ONS3ECard.signalPower[1] < PG.ABSTHR[1][0]))
							)
						{
							if(PG.HoldOFF[0] == 0)
							{
								MFSWSWToBackup();
								PG.SelectedPort[0] = PG.SecondaryPort[0];
								PG.ProtectionDirection[0] = ToSecondary;
								ONS3ECard.MFSWUpdateFlag = 1;	
								ONS3ECard.HOLDOFSWFlag = FALSE;	
								PG.SwitchReason[0] = SWCAUSE_SF;
							}	
							else
							{
								ONS3ECard.HOLDOFSWFlag = TRUE;
								PG.ProtectionDirection[0] = ToSecondary;
							}						
						}
					}				
				}
				else
				{
					if(PG.SelectedPort[0] == PG.SecondaryPort[0] && PG.BackSWMode[0] == ENABLE && ONS3ECard.BACKSWStartFlag == 0x00 && ONS3ECard.MFSWUpdateFlag == 0x00)
					{
						if( ONS3ECard.signalPower[0] > ( PG.ABSTHR[0][0] + alarmHysteresis.LOS[3] ) )
						{
							ONS3ECard.CanBackSwFlag[0] = TRUE;
								
							if(ONS3ECard.LSOKFlag == TRUE && ONS3ECard.CanBackSwFlag[1] == TRUE)
							ONS3ECard.BACKSWStartFlag = 0x01;	
						}
						else
						{
							ONS3ECard.CanBackSwFlag[0] = FALSE;
						}
					}
					else
					{
						ONS3ECard.CanBackSwFlag[0] = FALSE;
					}
				}
			}
		}
		else if(PG.AUTOSW_TYPE[0] == AUTOSWTYPE_OFFSET)
		{
			if(PG.SelectedPort[0] == PG.SecondaryPort[0])
			{
				if( (ONS3ECard.signalPowerPre > ( ONS3ECard.R2PowerPre + PG.OFFSETTHR[0][1] )) ||
						(ONS3ECard.signalPower[1] > ( AdcChannelData.POWER[1][5] + PG.OFFSETTHR[1][1] ))
					)
				{
					if(PG.HoldOFF[0] == 0)
					{
						MFSWSWToHost();
						PG.SelectedPort[0] = PG.PrimaryPort[0];
						PG.ProtectionDirection[0] = ToPrimary;
						ONS3ECard.MFSWUpdateFlag = 1;	
						ONS3ECard.HOLDOFSWFlag = FALSE;
						PG.SwitchReason[0] = SWCAUSE_SF;
					}
					else
					{
						ONS3ECard.HOLDOFSWFlag = TRUE;
						PG.ProtectionDirection[0] = ToPrimary;
					}
				}
			}
			else 
			{
				if( (ONS3ECard.R2PowerPre > ( ONS3ECard.signalPowerPre + PG.OFFSETTHR[0][0] )) ||
						(AdcChannelData.POWER[1][5] > ( ONS3ECard.signalPower[1] + PG.OFFSETTHR[1][0] ))
					)
				{
					if(PG.HoldOFF[0] == 0)
					{
						MFSWSWToBackup();
						PG.SelectedPort[0] = PG.SecondaryPort[0];
						PG.ProtectionDirection[0] = ToSecondary;
						ONS3ECard.MFSWUpdateFlag = 1;	
						ONS3ECard.HOLDOFSWFlag = FALSE;	
						PG.SwitchReason[0] = SWCAUSE_SF;
					}	
					else
					{
						ONS3ECard.HOLDOFSWFlag = TRUE;
						PG.ProtectionDirection[0] = ToSecondary;
					}				
				}
			}
		}
	}
}

//按键处理函数
//返回按键值
//mode:0不支持连续按;1支持连续按
//0 没有任何按键按下
//1 KEY1按下
//2 KEY2按下
//响应优先级，KEY>KEY2
u8 KEY_Scan(u8 mode)
{	 
	static u8 key_up=1;  //按键松开标志
	if(mode)key_up=1;    //支持连按
	if(key_up&&(KEY1_STA==0||KEY2_STA==0))
	{
		OSTimeDlyHMSM(0,0,0,10); 

		//ONS3ECard.KeyUpFlag = 0x00;
		key_up=0;
		if(KEY1_STA==0)return 1;
		else if(KEY2_STA==0)return 2;
	}else if(KEY1_STA==1&&KEY2_STA==1)
	{
		ONS3ECard.KeyUpFlag = 0x01;
		key_up=1; 	  
	}		
 	return 0;   //无按键按下
}

void KeyProcess( u8 key )
{
	if(PG.KEYEnable[0] == ON)
	{
		if(key == KEY1_PRES)     //OLP倒换类型更新 倒换顺序SF->Manual->Force->SF循环
		{
			//USARTPrint("Key1Process\r\n"  , 1); //USART1发送KEY1按键按下提示信息
			
			PG.KeySWFlag[0] = TRUE;
			
			if(PG.SwitchCMDTypeTemp == SWCAUSE_SF)
			{
				PG.SwitchCMDTypeTemp = SWCAUSE_MANUAL;
			}
			else if(PG.SwitchCMDTypeTemp == SWCAUSE_MANUAL)
			{
				PG.SwitchCMDTypeTemp = SWCAUSE_FORCE;
			}
			else
			{
				PG.SwitchCMDTypeTemp = SWCAUSE_SF;
			}
		
			
			if(ONS3ECard.LSOKFlag == TRUE)
			{
				ONS3ECard.SetDataBuff[FixedDataLen + 0] = PG.SwitchCMDTypeTemp;
				
				SetCmdhandle(OLPSWMODESet , 0x02 , 0x01 , USARTSET);			
			}
			else
			{
				SwitchCMDTypeProcess(0x01);
			}
		}
		else if(key == KEY2_PRES)   //根据OLP倒换类型进行相应的倒换操作
		{
			//USARTPrint("Key2Process\r\n"  , 1); //USART1发送KEY2按键按下提示信息

			PG.KeySWFlag[0] = TRUE;
			if(PG.SelectedPort[0] == PG.PrimaryPort[0])
			PG.ProtectionDirTemp = ToSecondary;
			else
			PG.ProtectionDirTemp = ToPrimary;
			
			if(PG.SwitchCMDType[0] == SWCAUSE_FORCE)
			{
				if(ONS3ECard.LSOKFlag == TRUE)
				{
					ONS3ECard.SetDataBuff[FixedDataLen + 0] = PG.ProtectionDirTemp;
					
					ONS3ECard.SYNCSWFlag = TRUE;
					SetCmdhandle(OLPSWDIRSet , 0x02 , 0x01 , USARTSET);	
				}
				else
				{
					ProtectionDirectionProcess(1);
				}
			}
			else if(PG.SwitchCMDType[0] == SWCAUSE_MANUAL)
			{
				ProtectionDirectionProcess(1);
			}
		}
	}
}


void MFSWProcess(void)
{
	//u8 ucI;
	
	if((TIMER_INTF(TIMER1)&0x0001) == 0x0001)
	{
		TIMER_INTF(TIMER1)&=0xfffe;		
		
		if(ONS3ECard.MFSWUpdateFlag == 1)
		{
			TIM2MFSWDATASave_Count++;
			if(TIM2MFSWDATASave_Count > 20)
			{
				TIM2MFSWDATASave_Count = 0;
				ONS3ECard.MFSWUpdateFlag = 0;
					
				//if(PG.SwitchCMDType[0] == SWCAUSE_CLEAR && PG.SwitchCMDTypePre[0] != SWCAUSE_CLEAR)
				//PG.SwitchReason[0] = SWCAUSE_CLEAR;
				//else
				//PG.SwitchReason[0] = SWCAUSE_SF;

					
				PG.SwitchCMDTypePre[0] = PG.SwitchCMDType[0];
				
				WriteEEPRom( ADDR_PG_SwitchCMDType, PG.SwitchCMDType[0] );
				WriteEEPRom( ADDR_PG_ProtectionDir, PG.ProtectionDirection[0] );
				WriteEEPRom( ADDR_PG_SelectPort, PG.SelectedPort[0] );
				ONS3ECard.EEPROMBuff[ADDR_PG_SwitchCMDType] = PG.SwitchCMDType[0];
				ONS3ECard.EEPROMBuff[ADDR_PG_ProtectionDir] =  PG.ProtectionDirection[0];
				ONS3ECard.EEPROMBuff[ADDR_PG_SelectPort] =  PG.SelectedPort[0];
				EEPROMdataCheck();
			}
		}
		
		if(ONS3ECard.LSTXDISABLEFlag == TRUE)
		{
			TIM2LSTXDISABLE_Count++;
			if(TIM2LSTXDISABLE_Count >= 1)
			{
				TIM2LSTXDISABLE_Count = 0;
					
				ONS3ECard.LSTXDISABLEFlag = FALSE;
			}
		}
		
		if(ONS3ECard.SYNCSWFlag == TRUE)
		{
			TIM2SYNCSW_Count++;
			if(TIM2SYNCSW_Count >= 100)
			{
				TIM2SYNCSW_Count = 0;				
				ONS3ECard.SYNCSWFlag = FALSE;
			}
		}
		
		
		if(ONS3ECard.SWMODERecoverStartFlag == TRUE)
		{
			TIM2SWMODEReover_Count[0]++;
			if(TIM2SWMODEReover_Count[0] >= PG.SWMODERecoverTime[0]*600)
			{
				TIM2SWMODEReover_Count[0] = 0;
					
				PG.SwitchCMDTypeTemp = SWCAUSE_SF;
				SwitchCMDTypeProcess(1);
			}
		}	
			
		if(ONS3ECard.BACKSWStartFlag == 0x01)
		{
			if(PG.SelectedPort[0] == PG.SecondaryPort[0])
			{
				TIM2BACK_SW_Count[0]++;
				if(TIM2BACK_SW_Count[0] >= PG.WaitToRestoreTime[0]*10)
				{
					TIM2BACK_SW_Count[0] = 0;
					ONS3ECard.BACKSWStartFlag = 0x00;
						
					PG.ProtectionDirTemp = ToPrimary;
					ONS3ECard.SetDataBuff[FixedDataLen + 0] = BackSwToPri;
					
					ONS3ECard.SYNCSWFlag = TRUE;
					SetCmdhandle(OLPSWDIRSet , 0x02 , 0x01 , USARTSET);	
							
				}
			}
			else
			{
				ONS3ECard.BACKSWStartFlag = 0x00;
				TIM2BACK_SW_Count[0] = 0x00;
			}
		}		
		
		//按键指示灯A/M/F
		if(PG.SwitchCMDType[0] == SWCAUSE_MANUAL)
		{
			LED_AF_Set_Low;
			
			ONS3ECard.LEDState.AFIND[0] = LED_STATA_OFF;
		}
		else if(PG.SwitchCMDType[0] == SWCAUSE_FORCE)
		{
			TIM2LEDAF_IND_Count++;
			if(TIM2LEDAF_IND_Count >= 2 )
			{
				TIM2LEDAF_IND_Count = 0;
				
				if(LED_AF_STA == 1)
				LED_AF_Set_Low;
				else
				LED_AF_Set_Hig;	
			}
			
			ONS3ECard.LEDState.AFIND[0] = LED_STATA_GreenFastBlink;
		}
		else
		{
			LED_AF_Set_Hig;
			
			ONS3ECard.LEDState.AFIND[0] = LED_STATA_GreenAlways;
		}
	}
}

void LineRoadLEDIndicateRefresh( void )
{
	if(PG.SelectedPort[0] == PG.PrimaryPort[0])
	{
		LED_Primary_IND;
		ONS3ECard.LEDState.PSSTAIND[0] = LED_STATA_GreenAlways;
	}
	else if(PG.SelectedPort[0] == PG.SecondaryPort[0])
	{
		LED_Secondary_IND;
		ONS3ECard.LEDState.PSSTAIND[0] = LED_STATA_OFF;
	}
}

void PORTLEDIndicateRefresh( void )
{
	if( ( SFFStatusInfo.alarmSTA.LOS_PD ) & ( 1 << 0 ) )
	{
		TX_LED_IND_Set_Low;
		ONS3ECard.LEDState.TXALRMIND[0] = LED_STATA_OFF;
	}
	else
	{
		TX_LED_IND_Set_Hig;
		ONS3ECard.LEDState.TXALRMIND[0] = LED_STATA_GreenAlways;
	}
	
	if( ( SFFStatusInfo.alarmSTA.LOS_PD ) & ( 1 << 3 ) )
	{
		R1_LED_IND_Set_Low;
		ONS3ECard.LEDState.R1ALRMIND[0] = LED_STATA_OFF;
	}
	else
	{
		R1_LED_IND_Set_Hig;
		ONS3ECard.LEDState.R1ALRMIND[0] = LED_STATA_GreenAlways;
	}
	
	if( ( SFFStatusInfo.alarmSTA.LOS_PD ) & ( 1 << 5 ) )
	{
		R2_LED_IND_Set_Low;
		ONS3ECard.LEDState.R2ALRMIND[0] = LED_STATA_OFF;
	}
	else
	{
		R2_LED_IND_Set_Hig;
		ONS3ECard.LEDState.R2ALRMIND[0] = LED_STATA_GreenAlways;
	}	
	
	if(ONS3ECard.FUN[0] == CardFUN_OLP_Z && ONS3ECard.LSOKFlag == TRUE)
	{
		LS_LED_IND_Set_Hig;
		PG.REMOTESTA[0] = 0x01;
		
		ONS3ECard.LEDState.LSLINKIND[0] = LED_STATA_GreenAlways;
	}
	else
	{
		LS_LED_IND_Set_Low;
		PG.REMOTESTA[0] = 0x00;
		
		ONS3ECard.LEDState.LSLINKIND[0] = LED_STATA_OFF;
	}
}

void SwitchCMDTypeProcess(u8 Index)
{
	switch( PG.SwitchCMDTypeTemp )
	{
		case  SWCAUSE_NO:
											PG.SwitchCMDType[Index - 1] = PG.SwitchCMDTypeTemp;
											PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
											break;
		case  SWCAUSE_SF:
											PG.SwitchCMDType[Index - 1] = PG.SwitchCMDTypeTemp;
											PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
											break;		
		case  SWCAUSE_MANUAL:
											PG.SwitchCMDType[Index - 1] = PG.SwitchCMDTypeTemp;
											PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
											break;	
		case  SWCAUSE_FORCE:
											PG.SwitchCMDType[Index - 1] = PG.SwitchCMDTypeTemp;
											PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
			
											PG.FAILALRM[Index - 1] = 0x00;
											break;			
		case  SWCAUSE_CLEAR:
											PG.SwitchCMDType[Index - 1] = PG.SwitchCMDTypeTemp;
											break;		
		case  SWCAUSE_WTR:
											PG.SwitchCMDType[Index - 1] = PG.SwitchCMDTypeTemp;
											PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
		
											PG.FAILALRM[Index - 1] = 0x00;
											break;	
		case  SWCAUSE_LOCK:
											PG.SwitchCMDType[Index - 1] = PG.SwitchCMDTypeTemp;
											PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
											//PG.SelectedPort[Index - 1] = PG.PrimaryPort[Index - 1];
											PG.ProtectionDirection[Index - 1] = ToPrimary;	
											
											PG.FAILALRM[Index - 1] = 0x00;
										
											if(PG.SelectedPort[Index - 1] != PG.PrimaryPort[Index - 1])
											{
												PG.SwitchALRM[Index - 1] = 0x01;
												PG.SwitchReason[Index - 1] = SWCAUSE_LOCK;
												PG.SelectedPort[Index - 1] = PG.PrimaryPort[Index - 1];
											}
											else
											{
												PG.SwitchReason[Index - 1] = SWCAUSE_NO;
											}														
											MFSWSWToHost();
											
											WriteEEPRom( ADDR_PG_ProtectionDir + Index - 1, PG.ProtectionDirection[Index - 1] );
											WriteEEPRom( ADDR_PG_SelectPort + Index - 1, PG.SelectedPort[Index - 1] );
											ONS3ECard.EEPROMBuff[ADDR_PG_ProtectionDir + Index - 1] = PG.ProtectionDirection[Index - 1];
											ONS3ECard.EEPROMBuff[ADDR_PG_SelectPort + Index - 1] = PG.SelectedPort[Index - 1];
											EEPROMdataCheck();
											break;			
			
		default:					break;
	}
	
	ONS3ECard.BACKSWStartFlag = 0x00;
	TIM2BACK_SW_Count[0] = 0;
	TIM2MFSWDATASave_Count = 0;
	ONS3ECard.MFSWUpdateFlag = 1;
	PG.SwitchReason[0] = SWCAUSE_NO;
	
	/*WriteEEPRom( ADDR_PG_SwitchCMDType + Index - 1, PG.SwitchCMDType[Index - 1] );
	ONS3ECard.EEPROMBuff[ADDR_PG_SwitchCMDType + Index - 1] = PG.SwitchCMDType[Index - 1];
	EEPROMdataCheck();
	
	if(PG.KeySWFlag[Index - 1] == TRUE)
	{
		PG.KeySWFlag[Index - 1] = FALSE;
	}*/
}

void ProtectionDirectionProcess(u8 Index)
{
	if(ONS3ECard.SYNCSWFlag == TRUE)
	{
		if(PG.ProtectionDirTemp == ToPrimary)
		{
			if(PG.SelectedPort[Index - 1] != PG.PrimaryPort[Index - 1])
			{
				MFSWSWToHost();
			}
		}
		else if(PG.ProtectionDirTemp == ToSecondary)
		{
			if(PG.SelectedPort[Index - 1] != PG.SecondaryPort[Index - 1])
			{
				MFSWSWToBackup();
			}
		}

		PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
		
		PG.ProtectionDirection[Index - 1] = PG.ProtectionDirTemp;
		
		if(PG.ProtectionDirTemp == ToPrimary)
		PG.SelectedPort[Index - 1] = PG.PrimaryPort[Index - 1];
		else
		PG.SelectedPort[Index - 1] = PG.SecondaryPort[Index - 1];
		
		//PG.SwitchReason[Index - 1] = SWCAUSE_SYNC;
		//PG.SwitchALRM[Index - 1] = 0x01;		
	}
	else if(PG.SwitchCMDType[Index - 1] == SWCAUSE_FORCE)
	{
		if(PG.ProtectionDirTemp == ToPrimary)
		{
			if(PG.SelectedPort[Index - 1] != PG.PrimaryPort[Index - 1])
			{			
				MFSWSWToHost();
				ONS3ECard.ForceSWFlag[Index - 1] = TRUE;
			}
		}
		else if(PG.ProtectionDirTemp == ToSecondary)
		{
			if(PG.SelectedPort[Index - 1] != PG.SecondaryPort[Index - 1])
			{			
				MFSWSWToBackup();
				ONS3ECard.ForceSWFlag[Index - 1] = TRUE;
			}
		}
		
		if(ONS3ECard.ForceSWFlag[Index - 1] == TRUE)
		{
			ONS3ECard.ForceSWFlag[Index - 1] = FALSE;
			
			PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
			
			PG.ProtectionDirection[Index - 1] = PG.ProtectionDirTemp;
			
			if(PG.ProtectionDirTemp == ToPrimary)
			PG.SelectedPort[Index - 1] = PG.PrimaryPort[Index - 1];
			else
			PG.SelectedPort[Index - 1] = PG.SecondaryPort[Index - 1];
			
			PG.SwitchReason[Index - 1] = SWCAUSE_FORCE;
			PG.SwitchALRM[Index - 1] = 0x01;
		}
	}
	else if(PG.SwitchCMDType[Index - 1] == SWCAUSE_MANUAL)
	{
		if(PG.ProtectionDirTemp == ToPrimary)
		{
			if(PG.SelectedPort[Index - 1] != PG.PrimaryPort[Index - 1])
			{
				if( ONS3ECard.signalPower[0] > ( PG.ABSTHR[0][0] + alarmHysteresis.LOS[3] ) )
				{
					MFSWSWToHost();
					
					ONS3ECard.ManualSWFlag[Index - 1] = TRUE;
				}
			}
		}
		else if(PG.ProtectionDirTemp == ToSecondary)
		{
			if(PG.SelectedPort[Index - 1] != PG.SecondaryPort[Index - 1])
			{
				if( AdcChannelData.POWER[0][5] > ( PG.ABSTHR[0][1] + alarmHysteresis.LOS[5] ) )
				{
					MFSWSWToBackup();
					
					ONS3ECard.ManualSWFlag[Index - 1] = TRUE;
				}
			}
		}
		
		if(ONS3ECard.ManualSWFlag[Index - 1] == TRUE)
		{
			ONS3ECard.ManualSWFlag[Index - 1] = FALSE;
			
			PG.SwitchCMDTypePre[Index - 1] = PG.SwitchCMDType[Index - 1];
			
			PG.ProtectionDirection[Index - 1] = PG.ProtectionDirTemp;
			
			if(PG.ProtectionDirTemp == ToPrimary)
			PG.SelectedPort[Index - 1] = PG.PrimaryPort[Index - 1];
			else
			PG.SelectedPort[Index - 1] = PG.SecondaryPort[Index - 1];
			
			PG.SwitchReason[Index - 1] = SWCAUSE_MANUAL;
			PG.SwitchALRM[Index - 1] = 0x01;
		}
	}
	
	ONS3ECard.BACKSWStartFlag = 0x00;
	TIM2BACK_SW_Count[0] = 0;
	TIM2MFSWDATASave_Count = 0;
	ONS3ECard.MFSWUpdateFlag = 1;
	
	/*if(PG.KeySWFlag[Index - 1] == TRUE)
	{
		PG.KeySWFlag[Index - 1] = FALSE;
	}*/
}


void SFFModulePRESTACheck(void)
{
	if(SFP1_PRE == 1)
	{
		ucSFFPresentAlarmCount++;
		if(ucSFFPresentAlarmCount > 2)
		{													
			ucSFFPresentAlarmCount = 10;
			ONS3ECard.SFFModule.PortPRESTA[0] = 0x00;											
		}
	}
	else
	{
		ONS3ECard.SFFModule.PortPRESTA[0] = 0x01;
		ucSFFPresentAlarmCount = 0;											
	}

	if( ( ONS3ECard.SFFModule.PortPRESTA[0] != ONS3ECard.SFFModule.PortLASTPRESTA[0] && ( ONS3ECard.SFFModule.PortPRESTA[0] == 0x01 )) )
	{
			SFFContdataGetFlag = ENABLE;
	}
		
	ONS3ECard.SFFModule.PortLASTPRESTA[0] = ONS3ECard.SFFModule.PortPRESTA[0];
}


void SFFModuleLaserControlON( void )
{
	SFP1_TXDISABLE_Set_Low;
}

void SFFModuleLaserControlOFF( void )
{
	SFP1_TXDISABLE_Set_Hig;
}


void SFFModuleLaserControl(void)
{
	if(ONS3ECard.LSTXDISABLEFlag == FALSE)
	{
		if( ONS3ECard.SFFModule.PortENABLE[0] == TRUE )
		{
			SFFModuleLaserControlON();
		}
		else
		{
			SFFModuleLaserControlOFF();
		}
	}
}

void SFFModuleDataGet(void)
{
	SFFRealdataGet_Count++;
	if(SFFRealdataGet_Count == 10)
	{
		SFFRealdataGet_Count = 0;
		SFFRealDataGet();
		if(SFFContdataGetFlag == ENABLE)
		{
			SFFConstDataGet();
			SFFConstdataGet_Count++;
			if(SFFConstdataGet_Count > 2)
			{
				SFFConstdataGet_Count = 0;
				SFFContdataGetFlag = DISABLE;
			}
		}
	}
}


void CANBusDataSend(u8 CANID,u8 DataLen)
{
	char ucI;
	char i,j;
	float k;
	

	if(CANID == CAN1ID)	
	{
		if((CAN_RFIFO0(CAN0)&0x03) == 0x00)
		{
			if((DataLen + 8) > 8)
			{
				i = (DataLen + 8) / 8;
				k = (float)(DataLen + 8) / 8;
				k = k - i;
				k = k * 8;
				j = (u8)k;
							
				for(ucI=0; ucI<i; ucI++)
				{
					//Delay(500000);    //3个发送缓冲都满的时候要延时等待
					CAN1_SendMessage(CANBus.SELF_FILTER_ID ,(u32)CAN_FF_EXTENDED,CANBus.TXData[CANID] + 8*ucI,8);
				}
							
				if(j  > 0)
				CAN1_SendMessage(CANBus.SELF_FILTER_ID ,(u32)CAN_FF_EXTENDED,CANBus.TXData[CANID] + 8*i,j);
			}
			else
			{
				CAN1_SendMessage(CANBus.SELF_FILTER_ID ,(u32)CAN_FF_EXTENDED,CANBus.TXData[CANID],(DataLen + 8));
			}
		}		
	}
	else if(CANID == CAN2ID)	
	{
		if((CAN_RFIFO1(CAN1)&0x03) == 0x00)
		{
			if((DataLen + 8) > 8)
			{
				i = (DataLen + 8) / 8;
				k = (float)(DataLen + 8) / 8;
				k = k - i;
				k = k * 8;
				j = (u8)k;
							
				for(ucI=0; ucI<i; ucI++)
				{
					//Delay(500000);    //3个发送缓冲都满的时候要延时等待
					CAN2_SendMessage(CANBus.SELF_FILTER_ID ,(u32)CAN_FF_EXTENDED,CANBus.TXData[CANID] + 8*ucI,8);
				}
							
				if(j  > 0)
				CAN2_SendMessage(CANBus.SELF_FILTER_ID ,(u32)CAN_FF_EXTENDED,CANBus.TXData[CANID] + 8*i,j);
			}
			else
			{
				CAN2_SendMessage(CANBus.SELF_FILTER_ID ,(u32)CAN_FF_EXTENDED,CANBus.TXData[CANID],(DataLen + 8));
			}
		}		
	}
}

void RemoteCardInfoGet( void )
{
	u8  SendDataTemp[10];
	USHORTVAR	TempData_u;
	
	ONS3ECard.TXSerialNum = 0;						
	ONS3ECard.SlotDataGetFlag = TRUE;
	ONS3ECard.TXCMDObjectCount = 0;
	
	SendDataTemp[0] = 0x00;
	SendDataTemp[1] = ONS3ECard.Addr[1];
	//SendDataTemp[2] = 0x00;
	//SendDataTemp[3] = 0x01;
	SendDataTemp[4] = 0x00;
	SendDataTemp[5] = 0x00;
		
					
	while(ONS3ECard.SlotDataGetFlag)
	{		
		//if(ONS3ECard.TXCMDObjectCount > 9)
		if(ONS3ECard.TXCMDObjectCount > 2)
		{
			//ONS3ECard.TXCMDObjectCount = 3;
			ONS3ECard.TXCMDObjectCount = 0;
			ONS3ECard.SlotDataGetFlag = FALSE;
			
			ONS3ECard.LSOKFlag = FALSE;
		}
		else
		{
			ONS3ECard.SlotDataGetFlag = TRUE;	
			if(ONS3ECard.TXCMDObjectCount >= 1)
			{
				//delay_ms(800);	
				delay_ms(300);				
			}
		}
		
		ONS3ECard.TXCMDObjectCount++;
			
		if(ONS3ECard.TXSerialNum  == 0x00)
		{
			ONS3ECard.TXObject = CardcmdObject[ONS3ECard.TXSerialNum];
			TempData_u.usWord = CardcmdObject[ONS3ECard.TXSerialNum];
			SendDataTemp[2] = TempData_u.ucByte[1];
			SendDataTemp[3] = TempData_u.ucByte[0];
			LSdataSend(SendDataTemp,6);
				
			delay_ms(100);
		}
		else if(ONS3ECard.TXSerialNum  > 0x02)
		{
			ONS3ECard.SlotDataGetFlag = FALSE;
		}
		else
		{
			if(ONS3ECard.TXObject == ONS3ECard.RXObject)
			{
				ONS3ECard.TXObject = CardcmdObject[ONS3ECard.TXSerialNum];
				TempData_u.usWord = CardcmdObject[ONS3ECard.TXSerialNum];
				SendDataTemp[2] = TempData_u.ucByte[1];
				SendDataTemp[3] = TempData_u.ucByte[0];
				LSdataSend(SendDataTemp,6);
							
				delay_ms(100);
			}
			else
			{
				//delay_ms(800);	
				delay_ms(300);
			
				LSdataSend(SendDataTemp,6);
			}
		}
	}	
}


void SlotCMDSetEXIT( void )
{

	ONS3ECard.SetObject = 0x0000;
	
	ONS3ECard.SetPort = 0x00;   //端口号port
	ONS3ECard.SetDataLen = 0x00;
	ONS3ECard.SetCMDFlag = FALSE;
	
	OSTaskResume( RemoteInfoGet_TASK_PRIO );  //恢复Remote信息获取任务
	OSTaskSuspend( SET_TASK_PRIO );			    	//挂起设置任务
}


void SetCmdhandle(u16 SetObject , u8 SetPort , u8 SetDataLen , u8 SetType)
{
	if(ONS3ECard.LSOKFlag == TRUE)
	{
		ONS3ECard.SetObject = SetObject;
		ONS3ECard.SetPort = SetPort;   //设置端口号
		ONS3ECard.SetDataLen = SetDataLen;
		
		ONS3ECard.SetDataBuff[0] = CMD_Set;
		ONS3ECard.SetDataBuff[1] = ONS3ECard.Addr[1];
		ONS3ECard.SetDataBuff[2] = (SetObject>>8)&0xff;
		ONS3ECard.SetDataBuff[3] = SetObject&0xff;
		ONS3ECard.SetDataBuff[4] = SetPort;
		ONS3ECard.SetDataBuff[5] = SetDataLen;
		
		ONS3ECard.SetCMDSendCount = 0;
		ONS3ECard.SetType = SetType;
		
		
		OSTaskResume( SET_TASK_PRIO );  							//恢复设置任务
		OSTaskSuspend( RemoteInfoGet_TASK_PRIO );			//挂起Remote信息获取任务
	}
}


void SlotCMDSet( void )
{	
	ONS3ECard.SetCMDFlag = TRUE;
	ONS3ECard.SetCMDSendCount = 0;
			
	while(ONS3ECard.SetCMDFlag)
	{		
		if(ONS3ECard.SetCMDSendCount > 2)
		{
			ONS3ECard.SetCMDSendCount = 0;
			//ONS3ECard.SetCMDFlag = FALSE;
			ONS3ECard.SetObjectFail = ONS3ECard.SetObject;
			
			SlotCMDSetEXIT();
		}
		else
		{
			//ONS3ECard.SetCMDFlag = TRUE;	
			if(ONS3ECard.SetCMDSendCount >= 1 && ONS3ECard.SetCMDSendCount <= 3)
			{
				if(ONS3ECard.SetObject != 0x0000)
				LSdataSend(&ONS3ECard.SetDataBuff[0],FixedDataLen + ONS3ECard.SetDataLen);
				
				delay_ms(500); 
			}
			else
			{
				if(ONS3ECard.SetObject != 0x0000)
				LSdataSend(&ONS3ECard.SetDataBuff[0],FixedDataLen + ONS3ECard.SetDataLen);
				
				delay_ms(150);
			}
		}
		
		ONS3ECard.SetCMDSendCount++;				
	}	
}


void InitTimer2 (void)
{
	/*TIMER1CLK = (SystemCoreClock / 4)*2 = 75MHz */
	
	timer_parameter_struct timer_initpara;
		
	timer_deinit(TIMER1);        //Ftim2=(Fsysclk/4)*2=90MHZ     
  /* initialize TIMER init parameter struct */
  timer_struct_para_init(&timer_initpara);
  /* TIMER1 configuration */
  timer_initpara.prescaler         = 7500-1;
  timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  timer_initpara.period            = 100*10-1;      //100表示TIM1 10ms中断一次
  timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  timer_init(TIMER1, &timer_initpara);
	
	// TIM2 counter disable 
	TIM2_Enable;
}

void InitTimer3 (void)
{
	timer_parameter_struct timer_initpara;
		
	timer_deinit(TIMER2);        //Ftim3=(Fsysclk/4)*2=75MHZ       
  /* initialize TIMER init parameter struct */
  timer_struct_para_init(&timer_initpara);
  /* TIMER2 configuration */
  timer_initpara.prescaler         = 7500-1;
  timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  timer_initpara.period            = 50-1;      //96表示TIM1 10ms中断一次
  timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  timer_init(TIMER2, &timer_initpara);
	
	// TIM3 counter disable 
	TIM3_Disable;
	
	/* TIM3 interrupt configuration */
	nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  nvic_irq_enable(TIMER2_IRQn, 3, 3);
  /* disable TIME3 interrupt */  
  TIM3_IT_Disable;
}


void ONS3ECardSLOTSTARead(void)
{
	ONS3ECard.SlotSTA[0] = BP_SLOT0_STA;
	ONS3ECard.SlotSTA[1] = BP_SLOT1_STA;
	ONS3ECard.SlotSTA[2] = BP_SLOT2_STA;
	ONS3ECard.SlotSTA[3] = BP_SLOT3_STA;
	ONS3ECard.SlotSTA[4] = BP_SLOT4_STA;
}

void CurSlotInfo(void)
{
	u8 ucI;

	if(CARD_INSERT_STA == 0)
	{
		//Delay_100us(10);
		OSTimeDlyHMSM(0,0,0,10);

		if(CARD_INSERT_STA == 0)
		{
			ONS3ECard.PlugSTA[0] = TRUE;
			ONS3ECardSLOTSTARead();
			for(ucI=0; ucI<5; ucI++)
			ONS3ECard.SlotSTA_Pre[ucI] = ONS3ECard.SlotSTA[ucI];
				
			//Delay_100us(10);
			OSTimeDlyHMSM(0,0,0,10);
			ONS3ECardSLOTSTARead();
				
			for(ucI=0; ucI<5; ucI++)
			{
				if(ONS3ECard.SlotSTA_Pre[ucI] != ONS3ECard.SlotSTA[ucI])
				break;
			}
				
			if(ucI == 5)
			{
				if(ONS3ECard.SlotSTA[0] == 1 && ONS3ECard.SlotSTA[1] == 1 && ONS3ECard.SlotSTA[2] == 1 && ONS3ECard.SlotSTA[3] == 1 && ONS3ECard.SlotSTA[4] == 1)
				{
					CANBus.SELF_FILTER_ID = 0;
					ONS3ECard.CurSlop = 0;					
					ONS3ECard.Addr[0] = ONS3ECard.CurSlop;
					ONS3ECard.PreSlop = ONS3ECard.CurSlop;						
				}
				else
				{
					ONS3ECard.CurSlop = (ONS3ECard.SlotSTA[4]<<4 | ONS3ECard.SlotSTA[3]<<3 | ONS3ECard.SlotSTA[2]<<2 | ONS3ECard.SlotSTA[1]<<1 | ONS3ECard.SlotSTA[0]<<0) + 1;
					//ONS3ECard.CurSlop = ONS3ECard.CurSlop + 1;
					if(ONS3ECard.PreSlop != ONS3ECard.CurSlop)
					{
						ONS3ECard.Addr[0] = ONS3ECard.CurSlop;
						ONS3ECard.PreSlop = ONS3ECard.CurSlop;
						CANBus.SELF_FILTER_ID = SELF_FILTER_ID0 + ONS3ECard.CurSlop;
						
						if(ONS3ECard.CurSlop <= 8)
						{
							ONS3ECard.CANID = CAN1ID;
							CAN2_BUS_Init(750);
							CAN1_BUS_Init(750);
						}
						else
						{
							ONS3ECard.CANID = CAN2ID;
							CAN1_BUS_Init(750);
							CAN2_BUS_Init(750);
						}
					}
				}
			}
		}
	}
	else
	{
		ONS3ECard.PlugSTA[0] = FALSE;
	}
}

void CurOSWSTAInfo(void)
{
	//TX OSW STA
	if(CUR_CH_TX == 0)
	{
		OSTimeDlyHMSM(0,0,0,10);

		if(CUR_CH_TX == 0)
		{
			PG.CURCH_TX[0] = 0x00;
		}
	}
	else
	{
		OSTimeDlyHMSM(0,0,0,10);

		if(CUR_CH_TX == 1)
		{
			PG.CURCH_TX[0] = 0x01;
		}
	}
	
	//RX OSW STA
	if(CUR_CH_RX == 0)
	{
		OSTimeDlyHMSM(0,0,0,10);

		if(CUR_CH_RX == 0)
		{
			PG.CURCH_RX[0] = 0x00;
		}
	}
	else
	{
		OSTimeDlyHMSM(0,0,0,10);

		if(CUR_CH_RX == 1)
		{
			PG.CURCH_RX[0] = 0x01;
		}
	}
}


void EEPROMProcess(void)
{
	if(ONS3ECard.EEPROMDataGetOKFlag == FALSE)
	{
		GetDataFromEEPROM();
		ONS3ECard.EEPROMGetCount++;
		
		if(ONS3ECard.EEPROMGetCount >= 3)
		{
			ONS3ECard.EEPROMGetCount = 0;
			GetEEPROMDataFromBackup();
		}
	}
		
	//EEPROM数据备份处理
	if(ONS3ECard.EEPROMUpdateFlag == TRUE)
	{
		EEPROMBackup_Count++;
					
		if(EEPROMBackup_Count >= 500)   //EEPROM数据有更新时5000ms后写入备份区
		{
			EEPROMBackup_Count = 0;
			
			EEPROMDATABackup();
			ONS3ECard.EEPROMUpdateFlag = FALSE;
		}
	}
}

void SF2507_Init(void)
{
	//Delay_100us(1000);
	//PORT0光口初始化
	SF2507_WriteTwoByte(0x3ba3,0x1ff9,0x03);  //MAC_SOFT_RST 软件复位PORT0和PORT1
	SF2507_WriteTwoByte(0x3d0d,0x0000,0x03);  //P0LED1复用于SFP_LOS功能
	SF2507_WriteTwoByte(0x3ba5,0x0005,0x03);  //Fiber mode enable for port0
	//SF2507_WriteTwoByte(0x3ba6,0x0005,0x03);	//Fiber mode enable for port1
	
	SF2507_WriteTwoByte(0x1f03,0x0768,0x03);	//间接写的数据
	SF2507_WriteTwoByte(0x1f02,0x0012,0x03);	//PORT0间接写的PHY地址18
	SF2507_WriteTwoByte(0x1f00,0x0003,0x03);	//间接写操作
	
	//SF2507_WriteTwoByte(0x1f03,0x0768,0x03);	//间接写的数据
	//SF2507_WriteTwoByte(0x1f02,0x0032,0x03);	//PORT1间接写的PHY地址18
	//SF2507_WriteTwoByte(0x1f00,0x0003,0x03);	//间接写操作
	
	SF2507_WriteTwoByte(0x3ba3,0x1fff,0x03);  //MAC_SOFT_RST 软件复位释放所有的端口
	
	//PORT6 RMII_PHY模式初始化
	SF2507_WriteTwoByte(0x1305,0x0080,0x03);
	SF2507_WriteTwoByte(0x1311,0x1005,0x03);
	SF2507_WriteTwoByte(0x3b0c,0x0003,0x03);
}

void StoreOperateModeSwitch(void)
{	
	if ( SGM3157_STA == 1 )
	{
		switchSGM3157 = HIGH;
	}
	else
	{
		switchSGM3157 = LOW;
	}
}

void RestoreOperateModeSwitch(void)
{	
	if ( switchSGM3157 == LOW  )
	{
		SGM3157_Set_Low;
	}
	else
	{
		SGM3157_Set_Hig;
	}
}

void OGBMute( void )
{
	OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,0);

	PUMP1_OFF;
	
	ONS3ECard.pumpStatus = PUMP_OFF;
	ONS3ECard.PreOpMode = DISABLE_MODE;

}

void AutoShutdown( void )		
{
	if( alarmInfo.TMP == ENABLE )
	{
		OGBMute();
	}
	else
	{
		if( ONS3ECard.pumpStatus == PUMP_ON  )
		{
			if( ONS3ECard.SoftwareShutFlag == ENABLE )
			{
				OGBMute();
			}
			#if 1
	 		else if( ONS3ECard.OpMode[0] != MANUAL_MODE )
			{
				if( alarmInfo.Low == ENABLE )
				{
					OGBMute();
				}
			}
			#endif
			else
			{
				return;
			}
		}
		else
		{
			if( ONS3ECard.SoftwareShutFlag == DISABLE )
			{
				#if 1
				if( alarmInfo.Low == DISABLE || ONS3ECard.OpMode[0] == MANUAL_MODE )
				{
					ONS3ECard.pumpStatus = PUMP_ON;
				}
				#endif	
			}
		}
	}
}

short GetAPCTraceDelta( short powerOffset ,short targetPower ,USHORT modulePowerTrace)
{
	short delta=0;

	if( powerOffset > 10 )
	{
		if( powerOffset > 200 )
		{
			if( powerOffset > 400 )
			{
				if( powerOffset > 1600 )
				{
					delta = modulePowerTrace * 1 / 100;
				}
				else
				{
					//delta = modulePowerTrace * 2 / 100;
					delta = modulePowerTrace * 1 / 400;
				}
			}
			else
			{
				//delta = modulePowerTrace * 2 / 100;
				delta = modulePowerTrace * 1 / 800;
			}
		}
		else
		{
			if( powerOffset > 100 )
			{
				//delta = modulePowerTrace * 1 / 100;
				delta = modulePowerTrace * 1 / 1000;
			}
			else
			{
				if( targetPower > PumpPowerMaxValue - 5 )
				{
					delta = modulePowerTrace * 1 / 1000;
				}
				else
				{
					delta = 8;
				}
			}
		}
	}
	else
	{
		if( powerOffset < -10 )
		{
			if ( powerOffset < -200 )
			{
				if ( powerOffset < -400 )
				{
					if( targetPower > PumpPowerMaxValue - 7 )
					{
						//delta = modulePowerTrace * 4 / 100;
						delta = modulePowerTrace * 1 / 100;
					}
					else
					{
						//delta = modulePowerTrace * 1 / 100;
						delta = modulePowerTrace * 1 / 200;
					}
				}
				else
				{
					//delta = modulePowerTrace * 5 / 1000;
					delta = modulePowerTrace * 1 / 400;
				}
			}
			else
			{
				if ( powerOffset < -100 )
				{
					//delta = modulePowerTrace * 3 / 1000;
					delta = modulePowerTrace * 1 / 800;
				}
				else
				{
					if( targetPower > PumpPowerMaxValue - 7 )
					{
						delta = modulePowerTrace * 1 / 1000;
					}
					else
					{
						delta = 8;
					}
				}
			}
		}
	}
	if( delta < 8 )
	{
		delta = 2;
	}
	return( delta );
}


void ramanpump1PowerTrace( short targetPower )
{
	short powerOffset;
	USHORT temp;
	short delta;
	
	if( ONS3ECard.pumpStatus == PUMP_OFF )
	{
		return;
	}

	powerOffset =  10*( targetPower - AdcChannelData.PumpOp[0] );
	modulePowerTrace = pump1PowerTrace;

	if( powerOffset > 50 )
	{
		delta = GetAPCTraceDelta( powerOffset,targetPower,modulePowerTrace );
		temp = ( long )delta + pump1PowerTrace;
		if( temp >= MAXAPCVALUE )
		{
			pump1PowerTrace = MAXAPCVALUE;
		}
		else
		{
			pump1PowerTrace = ( USHORT )temp;
		}

		OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,pump1PowerTrace);
	}
	else
	{
		if( powerOffset < -50 )
		{	
			delta = GetAPCTraceDelta( powerOffset,targetPower,modulePowerTrace );
			temp = ( long )pump1PowerTrace - delta;
			if( temp <= MINAPCVALUE )
			{
				pump1PowerTrace = MINAPCVALUE;
			}			
			else
			{
				pump1PowerTrace = ( USHORT )temp;
			}

			OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,pump1PowerTrace);
		}
	}	
	//delay_ms(2);
	if((powerOffset >= -50 )&&(powerOffset <= 50))
	{
		RamanPump1BallenceFlag = TRUE;
	}
	else
	{
		RamanPump1BallenceFlag = FALSE;
	}
}


UCHAR doSwitchAPCmode( void )
{
	float Temp_Vol;
	
	if( ONS3ECard.PreOpMode != POWER_MODE )
	{
		//OGBMute();
		if(ONS3ECard.PreOpMode == DISABLE_MODE)
		{
			pump1PowerTrace = 1000;
		}
		else
		{		
			if(ONS3ECard.pumpNum == '1')  //P1
			{
				Temp_Vol = (float)ONS3ECard.wave1Powerset[0]*ONS3ECard.Pump1K / 1000;
				pump1PowerTrace = (u16)Temp_Vol*16;
			  //pump1PowerTrace = ADCSample.Type.P1OpCode*16*33/41;
				OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,pump1PowerTrace);
				
			}
			
		}
		ONS3ECard.PreOpMode = ONS3ECard.OpMode[0];
		SGM3157_Set_Low; 

		return( ERROR );
	}
	return( SUCCESS );
}

void ramanAPPCTrace( void )
{
	if( ONS3ECard.pumpStatus == PUMP_OFF )
	{
		return;
	}

	if( doSwitchAPCmode() != SUCCESS )
	{
		return;	
	}
	if(( ONS3ECard.waveNum == '1' )&&( ONS3ECard.pumpNum == '1' ))//P1\P2
	{
		if( ONS3ECard.wave1Powerset[0] > 0 )
		{
			PUMP1_ON;  
		}
		else
		{
			PUMP1_OFF;
		}

		ramanpump1PowerTrace(ONS3ECard.wave1Powerset[0] );
		
		if( RamanPump1BallenceFlag == TRUE)
		{
			RamanBallenceFlag = ENABLE;
		}
		else
		{
			RamanBallenceFlag = DISABLE;
		}
		
	}
}
void ramanpumpGainTrace( short targetPower )
{
	float Temp_Vol;
	
	Temp_Vol = (float)targetPower*ONS3ECard.Pump1K / 1000;
	pump1PowerTrace = (u16)Temp_Vol*16;
	OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,pump1PowerTrace);
}

UCHAR doSwitchAGCmode( void )
{
	if( ONS3ECard.PreOpMode != GAIN_MODE )
	{
		ONS3ECard.PreOpMode = ONS3ECard.OpMode[0];
		SGM3157_Set_Low; 
		
		return( ERROR );
	}
	ramanpumpGainTrace( ONS3ECard.ramanTargetPower );
	PUMP1_ON;
	return( SUCCESS );
}

void ramanAGCTrace( void )
{
	short offset;
	
	if( ONS3ECard.pumpStatus == PUMP_OFF )
	{
		return;
	}
	if( doSwitchAGCmode() != SUCCESS )
	{
		return;	
	}

	offset = ONS3ECard.ramanTargetPower - AdcChannelData.PumpOp[0];
	if( offset > 15 )
	{
		ONS3ECard.ramanTargetPower++;
		ramanpumpGainTrace( ONS3ECard.ramanTargetPower );
	}
	else if( offset< -15 )
	{
		ONS3ECard.ramanTargetPower--;
		ramanpumpGainTrace( ONS3ECard.ramanTargetPower );
	}
	else
	{
		RamanPump1BallenceFlag = TRUE;
	}
}


UCHAR doSwitchACCmode( void )
{
	if( ONS3ECard.PreOpMode != MANUAL_MODE )
	{
		if(ONS3ECard.pumpNum == '1')  //P1,
		{
			Pump1CurrentTrace = ADCSample.Type.P1_ILD_J1*16*25/41;
			OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,Pump1CurrentTrace);
		}

			
		ONS3ECard.PreOpMode = ONS3ECard.OpMode[0];
		SGM3157_Set_Hig; 

		return( ERROR );
	 }
	return( SUCCESS );
}


/*PUMP1 ACC工作模式处理*/
void ramanpump1ManualTrace( void )
{
  short temp;
  long DeltaTrace;	
	long targetTrace;
	
	if( ONS3ECard.pumpStatus == PUMP_OFF  )
	{
		return;
	}
	
	temp = AdcChannelData.PumpILD[0] - ONS3ECard.pump1ISPSet[0];
		
	DeltaTrace = ( ( long )temp * ONS3ECard.Pump1CaltargetACCK ) / 2000;
	targetTrace = Pump1CurrentTrace - DeltaTrace;
	if( temp < -10 )
	{
		if( temp < -20 )
		{
			if( temp < -50 )
			{
				if ( targetTrace < MAXPUMPILDTRACE )
				{
					Pump1CurrentTrace = targetTrace;						
				}
				else
				{
					Pump1CurrentTrace = MAXPUMPILDTRACE;
				}
			}
			else
			{
				if ( Pump1CurrentTrace < MAXPUMPILDTRACE )
				{
					Pump1CurrentTrace = Pump1CurrentTrace + 26;			
				}
				else
				{
					Pump1CurrentTrace = MAXPUMPILDTRACE;
				}
			}
		}
		else
		{
			if ( Pump1CurrentTrace < MAXPUMPILDTRACE )
			{
				Pump1CurrentTrace = Pump1CurrentTrace + 3;			
			}
			else
			{
				Pump1CurrentTrace = MAXPUMPILDTRACE;
			}
			
		}
		OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,Pump1CurrentTrace);
	}
	else
	{
		if( temp > 10 )
		{
			if( temp > 50 )
			{
				if ( targetTrace > MINPUMPILDTRACE )
				{
					Pump1CurrentTrace = targetTrace;			
				}
				else
				{
					Pump1CurrentTrace = MINPUMPILDTRACE;
				}
			}
			else
			{
				if( temp > 20 )
				{
					if ( Pump1CurrentTrace > MINPUMPILDTRACE )
					{
						Pump1CurrentTrace = Pump1CurrentTrace - 26;
					}
					else
					{
						Pump1CurrentTrace = MINPUMPILDTRACE;
					}
				}
				else
				{	
					if ( Pump1CurrentTrace > MINPUMPILDTRACE )
					{
						Pump1CurrentTrace = Pump1CurrentTrace - 3;
					}
					else
					{
						Pump1CurrentTrace = MINPUMPILDTRACE;
					}
				}					
			}
			OutputTPC116S8(DAC_TPC116S8,VOUT_PUMP1Drive,Pump1CurrentTrace);
		}
	} 	
}

void ramanACCTrace( void )
{
	if( ONS3ECard.pumpStatus == PUMP_OFF )
	{
		return;
	}

	if( doSwitchACCmode() != SUCCESS )
	{
		return;	
	}
	if(( ONS3ECard.waveNum == '1' )&&( ONS3ECard.pumpNum == '1' ))//P1/P2
	{
		if(ONS3ECard.pump1ISPSet == 0)
		{
			PUMP1_OFF;
		}
		else
		{
			PUMP1_ON;  
			ramanpump1ManualTrace();
		}
	}
}

void  BSP_Init(void)
{
  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/   
  /* RCC system reset(for debug purpose) */
  RCC_Configuration();
  GPIO_Configuration();    
  adc_config(); 	// ADC configuration 
	sys_delay_init(150);
	SFF_Init();		//IIC初始化 	
	MisDeviceInit();
	sysRamInit();
  setBaud(baudRateValue);
	InitTimer2();
	InitTimer3();	
	
	mymem_init(SRAMIN);  	//初始化内部内存池
	mymem_init(SRAMCCM); 	//初始化CCM内存池
	
	if ( cStartFlag[0] != 0x12 || cStartFlag[1] != 0x34 )
	{
		sysRamClear();		
	}
	else
	{
		RestoreOperateModeSwitch();
	}

	cStartFlag[0] = 0x12;
	cStartFlag[1] = 0x34;
	
	delay_init(150);  //初始化延时函数
}








